简体   繁体   English

在 JQuery 中获取 Div 值

[英]Get a Div Value in JQuery

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?我将如何通过 JQuery 或标准 JS 检索值(“某些值”)? I tried:我试过了:

var mb = document.getElementById("myDiv");

But the debugger console shows "mb is null".但是调试器控制台显示“mb 为空”。 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.这是 JQuery 事件处理程序的一部分,我试图在单击按钮时读取该值。 The handler function is working but it can't interpret the jQuery value it seems:处理程序函数正在工作,但它似乎无法解释 jQuery 值:

jQuery('#gregsButton').click(function() { 
   var mb = $('#myDiv').text();
   alert("Value of div is: " + mb.value); 
});
$('#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 文件:)

myDivObj = document.getElementById("myDiv");
if ( myDivObj ) {
   alert ( myDivObj.innerHTML ); 
}else{
   alert ( "Alien Found" );
}

Above code will show the innerHTML, ie if you have used html tags inside div then it will show even those too.上面的代码将显示innerHTML,即如果您在div 中使用了html 标签,那么它也会显示那些。 probably this is not what you expected.可能这不是您所期望的。 So another solution is to use: innerText / textContent property [ thanx to bobince, see his comment ]所以另一个解决方案是使用: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 );
                } 
            }  
        }

if you div looks like this:如果你 div 看起来像这样:

<div id="someId">Some Value</div>

you could retrieve it with jquery like this:你可以像这样用 jquery 检索它:

$('#someId').text()

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> 

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.请记住,在事件处理程序之外无法访问变量“mb”。 Also, the text() function returns a string, no need to get mb.value.此外,text() 函数返回一个字符串,无需获取 mb.value。

您还可以使用 innerhtml 来获取标签内的值....

You can do get id value by using您可以通过使用获取 id 值

 test_alert = $('#myDiv').val(); alert(test_alert);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="myDiv"><p>Some Text</p></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM