简体   繁体   English

在Div Javascript中获取价值

[英]Get value inside Div Javascript

ho,

I have a div that I access like so: 我有一个div,我这样访问:

 var gridcellrowvalue0 = gridcell0.innerHTML;

This returns to me the following div: 这将返回给我以下div:

 <div class="DivOverflowNoWrap Ellipsis" style="width:100%;" data-textwidth="50" data-originaltext="DefaultText" data-ingrid="1">DefaultText</div>

In my JS I would like to accesss the "DefaultText" variable and I have tried this: 在我的JS中,我想访问"DefaultText"变量,我试过这个:

 gridcellrowvalue0.innerHTML;
 gridcellrowvalue0.getAttribute("data-originaltext");

But none of them work. 但它们都不起作用。 I'm assuming that getAttribute doesn't work because it is not really an element, it's innerhtml. 我假设getAttribute不起作用,因为它不是真正的元素,它是innerhtml。

My goal is to use the "DefaultText" value in an IF-statement and therefore I simply need it. 我的目标是在IF语句中使用"DefaultText"值,因此我只需要它。

I appreciate any pointers, my friends! 我感谢任何指点,我的朋友们!

You could access your element directly from gridcell0 using gridcell0.querySelector('.DivOverflowNoWrap') instead, like : 您可以使用gridcell0.querySelector('.DivOverflowNoWrap')直接从gridcell0访问您的元素,如:

var gridcell0 = document.querySelector('#x');
console.log( gridcell0.querySelector('.DivOverflowNoWrap').innerHTML );

Hope this helps. 希望这可以帮助。

 var gridcell0 = document.querySelector('#x'); if (gridcell0.querySelector('.DivOverflowNoWrap') !== null) { console.log(gridcell0.querySelector('.DivOverflowNoWrap').innerHTML); } else { console.log('Does not exist'); } 
 <div id="x"> <div class="DivOverflowNoWrap Ellipsis" style="width:100%;" data-textwidth="50" data-originaltext="DefaultText" data-ingrid="1">DefaultText</div> </div> 

With Javascript also it can be achieved but I am showing here using jQuery 使用Javascript也可以实现,但我在这里使用jQuery显示

 $('document').ready(function() { var div = $(".DivOverflowNoWrap"); var text = div.text(); alert(text); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="DivOverflowNoWrap Ellipsis" style="width:100%;" data-textwidth="50" data-originaltext="DefaultText" data-ingrid="1">DefaultText</div> 

The problem is how you access the div in the first place. 问题是你如何首先访问div。 If you do it like you described (with gridcell0.innerHTML ). 如果你像你描述的那样(使用gridcell0.innerHTML )。 It will return a string. 它将返回一个字符串。 Not an HTML element. 不是HTML元素。

Therefore you can't use .getAttribute or .innerHTML , because you try to apply it on a string. 因此,您不能使用.getAttribute.innerHTML ,因为您尝试将其应用于字符串。 Access your div differently ( querySelector or getElementBy... ) and you will be able to use those. 以不同方式访问您的div( querySelectorgetElementBy... ),您将能够使用它们。

You can use jquery: 你可以使用jquery:

$("[class='DivOverflowNoWrap']").text();


$("[class='DivOverflowNoWrap']").attr("data-originaltext")

It's pretty simple: 这很简单:

 <html><head></head> <div class="DivOverflowNoWrap Ellipsis" style="width:100%;" data-textwidth="50" data-originaltext="DefaultText" data-ingrid="1">DefaultText</div> <script> test(); function test(){ var x=document.getElementsByClassName("DivOverflowNoWrap Ellipsis")[0].getAttribute("data-originaltext"); alert(x); } </script> </html> 

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

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