jQuery 如何在警告框中显示变量值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16623575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to display variable value in alert box?
提问by user2139497
I wanted to display variable value in alert box.
我想在警报框中显示变量值。
please see below code :
请看下面的代码:
<script>
function check() {
var content = document.getElementById("one").value;
alert(content);
}
</script>
<body onload="check()">
<div style="position: absolute; z-index: 9; width: 450px; top: 38px; margin-left: 176px;">
<style>
div#layout {
margin:0px;
padding:px;
width:450px;
margin:0px auto;
overflow:hidden;
}
</style>
<div id="layout">
<span id="one" style="display:none" ph="layout1" class="ione">yes</span>
<span id="two" style="display:none" ph="layout1" class="ione">no</span>
</div>
</div>
</body>
When i am executing this code then it is showing value as undefined in alert box. value in span id"one"is changing in different different situation. i want to track every time...so i cant hardcode it.
当我执行此代码时,它在警报框中显示值未定义。跨度 id"one" 中的值在不同的情况下会发生变化。我想每次都跟踪...所以我不能对其进行硬编码。
can you please help in this?
你能帮忙吗?
回答by PSR
spans not have the value in html
跨度在 html 中没有值
one is the id
for span
tag
一个是id
forspan
标签
in javascript use
在 javascript 中使用
document.getElementById('one').innerText;
in jQuery use
在 jQuery 中使用
$('#one').text()
function check() {
var content = document.getElementById("one").innerText;
alert(content);
}
or
或者
function check() {
var content = $('#one').text();
alert(content);
}
回答by PSR
$(document).ready(function(){
// alert("test");
$("#name").click(function(){
var content = document.getElementById("ghufran").innerHTML ;
alert(content);
});
//var content = $('#one').text();
})
there u go buddy this code actually works
伙计,你去吧,这段代码确实有效
回答by Abhijeet
document.getElementById('one').innerText;
alert(content);
It does not print the value; But, if done this way
它不打印值;但是,如果这样做
document.getElementById('one').value;
alert(content);
回答by Yeronimo
Try innerText property:
尝试 innerText 属性:
var content = document.getElementById("one").innerText;
alert(content);
See also this fiddle http://fiddle.jshell.net/4g8vb/
回答by anomaaly
Clean way with no jQuery:
没有 jQuery 的干净方式:
function check(some_id) {
var content = document.getElementById(some_id).childNodes[0].nodeValue;
alert(content);
}
This is assuming each span has only the value as a child and no embedded HTML.
这是假设每个跨度只有作为子级的值并且没有嵌入的 HTML。