I have a page containing the following div element:
我有一个包含以下div元素的页面:
<div id="myDiv" class="myDivClass" style="">Some Value</div>
How would I retrieve the value ("Some Value") either through JQuery or through standard JS? I tried:
如何通过JQuery或标准JS检索值(“Some Value”)?我试过了:
var mb = document.getElementById("myDiv");
But the debugger console shows "mb is null". Just wondering how to retrieve this value.
但调试器控制台显示“mb为null”。只是想知道如何检索这个值。
---- UPDATE ---- When I try the suggestion I get: $ is not a function
---- UPDATE ----当我尝试这个建议时,我得到:$不是一个函数
This is part of a JQuery event handler where I am trying to read the value when I click a button. The handler function is working but it can't interpret the jQuery value it seems:
这是JQuery事件处理程序的一部分,我在单击按钮时尝试读取值。处理程序函数正在运行,但它无法解释它看起来的jQuery值:
jQuery('#gregsButton').click(function() {
var mb = $('#myDiv').text();
alert("Value of div is: " + mb.value);
});
6 个解决方案
#1
20
myDivObj = document.getElementById("myDiv");
if ( myDivObj ) {
alert ( myDivObj.innerHTML );
}else{
alert ( "Alien Found" );
}
Above code will show the innerHTML, i.e if you have used html tags inside div then it will show even those too. probably this is not what you expected. So another solution is to use: innerText / textContent property [ thanx to bobince, see his comment ]
上面的代码将显示innerHTML,即如果你在div中使用了html标签,那么它也会显示出来。可能这不是你所期望的。所以另一种解决方案是使用:innerText / textContent属性[thanx to bobince,查看他的评论]
function showDivText(){
divObj = document.getElementById("myDiv");
if ( divObj ){
if ( divObj.textContent ){ // FF
alert ( divObj.textContent );
}else{ // IE
alert ( divObj.innerText ); //alert ( divObj.innerHTML );
}
}
}
#2
56
$('#myDiv').text()
Although you'd be better off doing something like:
虽然你最好做以下事情:
var txt = $('#myDiv p').text();
alert(txt);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv"><p>Some Text</p></div>
Make sure you're linking to your jQuery file too :)
确保你也链接到你的jQuery文件:)
#3
12
if you div looks like this:
如果你看起来像这样:
<div id="someId">Some Value</div>
you could retrieve it with jquery like this:
你可以用这样的jquery检索它:
$('#someId').text()
#4
6
your div looks like this:
你的div看起来像这样:
<div id="someId">Some Value</div>
With jquery:
使用jquery:
<script type="text/javascript">
$(function(){
var text = $('#someId').html();
//or
var text = $('#someId').text();
};
</script>
#5
2
You could use
你可以用
jQuery('#gregsButton').click(function() {
var mb = jQuery('#myDiv').text();
alert("Value of div is: " + mb);
});
Looks like there may be a conflict with using the $. Remember that the variable 'mb' will not be accessible outside of the event handler. Also, the text() function returns a string, no need to get mb.value.
看起来使用$可能存在冲突。请记住,变量'mb'在事件处理程序之外是不可访问的。另外,text()函数返回一个字符串,不需要获取mb.value。
#6
1
You could also use innerhtml to get the value within the tag....
你也可以使用innerhtml获取标签内的值....
#1
20
myDivObj = document.getElementById("myDiv");
if ( myDivObj ) {
alert ( myDivObj.innerHTML );
}else{
alert ( "Alien Found" );
}
Above code will show the innerHTML, i.e if you have used html tags inside div then it will show even those too. probably this is not what you expected. So another solution is to use: innerText / textContent property [ thanx to bobince, see his comment ]
上面的代码将显示innerHTML,即如果你在div中使用了html标签,那么它也会显示出来。可能这不是你所期望的。所以另一种解决方案是使用:innerText / textContent属性[thanx to bobince,查看他的评论]
function showDivText(){
divObj = document.getElementById("myDiv");
if ( divObj ){
if ( divObj.textContent ){ // FF
alert ( divObj.textContent );
}else{ // IE
alert ( divObj.innerText ); //alert ( divObj.innerHTML );
}
}
}
#2
56
$('#myDiv').text()
Although you'd be better off doing something like:
虽然你最好做以下事情:
var txt = $('#myDiv p').text();
alert(txt);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv"><p>Some Text</p></div>
Make sure you're linking to your jQuery file too :)
确保你也链接到你的jQuery文件:)
#3
12
if you div looks like this:
如果你看起来像这样:
<div id="someId">Some Value</div>
you could retrieve it with jquery like this:
你可以用这样的jquery检索它:
$('#someId').text()
#4
6
your div looks like this:
你的div看起来像这样:
<div id="someId">Some Value</div>
With jquery:
使用jquery:
<script type="text/javascript">
$(function(){
var text = $('#someId').html();
//or
var text = $('#someId').text();
};
</script>
#5
2
You could use
你可以用
jQuery('#gregsButton').click(function() {
var mb = jQuery('#myDiv').text();
alert("Value of div is: " + mb);
});
Looks like there may be a conflict with using the $. Remember that the variable 'mb' will not be accessible outside of the event handler. Also, the text() function returns a string, no need to get mb.value.
看起来使用$可能存在冲突。请记住,变量'mb'在事件处理程序之外是不可访问的。另外,text()函数返回一个字符串,不需要获取mb.value。
#6
1
You could also use innerhtml to get the value within the tag....
你也可以使用innerhtml获取标签内的值....