在JQuery中获得Div值 [英] Get a Div Value in JQuery
问题描述
< div id =myDivclass =myDivClass style =>某些值< / div>
我如何通过JQuery或标准JS检索值(Some Value)?我试过:
var mb = document.getElementById(myDiv);
但调试器控制台显示mb is null。
---- UPDATE ----
当我尝试建议时,我得到:$不是函数
这是一个JQuery事件处理程序的一部分,我正在尝试在单击按钮时读取该值。处理函数正在工作,但它不能解释看起来像jQuery的值:
$ p $ jQuery('#gregsButton')。click (函数(){
var mb = $('#myDiv')。text();
alert(div的值为:+ mb.value);
});
myDivObj =的document.getElementById( myDiv);
if(myDivObj){
alert(myDivObj.innerHTML);
} else {
alert(Alien Found);
}
以上代码将显示innerHTML,即如果您在div内使用了html标签那么它也会显示出来。可能这不是你所期望的。因此,另一个解决方案是使用:innerText / textContent属性[thanx to bobince,查看他的评论]
$ b $ pre $ function showDivText(){
divObj = document.getElementById(myDiv);
if(divObj){
if(divObj.textContent){// FF
alert(divObj.textContent);
} else {// IE
alert(divObj.innerText); // alert(divObj.innerHTML);
}
}
}
I have a page containing the following div element:
<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:
var mb = document.getElementById("myDiv");
But the debugger console shows "mb is null". Just wondering how to retrieve this value.
---- UPDATE ---- When I try the suggestion I get: $ is not a function
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('#gregsButton').click(function() {
var mb = $('#myDiv').text();
alert("Value of div is: " + mb.value);
});
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 ]
function showDivText(){
divObj = document.getElementById("myDiv");
if ( divObj ){
if ( divObj.textContent ){ // FF
alert ( divObj.textContent );
}else{ // IE
alert ( divObj.innerText ); //alert ( divObj.innerHTML );
}
}
}
这篇关于在JQuery中获得Div值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!