Javascript 从 h4 标签中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16373372/
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
Javascript get the value from an h4 Tag
提问by Sam
i am trying to get the value Lotusof the h4tag from the code below
我想获得价值莲花的的H4从下面的代码标签
<a id="linkSale" class="linkAccess" title="" href="/vp4/Home/Handlers/OperationAccess.ashx?operationid=17285" onclick="javascript:PrepareEventOnLinksAnchor(17285);">
<h4>Lotus</h4>
<p class="dateSales">Du <em><strong>mercredi 1 mai</strong> 9h</em> au <em><strong>dimanche 5 mai</strong> 6h</em> </p>
<p class="baseline"></p>
</a>
to do that i did as follow :
要做到这一点,我做了如下:
var h=document.getElementById("17285");
var h=document.getElementById("17285");
var i=h.getElementsByTagName("h4");
var i=h.getElementsByTagName("h4");
which returned the following line :
返回以下行:
<h4>Lotus</h4>
what I want is to get the value Lotus
converted to a text.
我想要的是将值Lotus
转换为文本。
回答by mplungjan
document.getElementById("linksale").getElementsByTagName("h4")[0].innerHTML
will work
将工作
回答by mplungjan
you can use jQuery for that :
你可以使用jQuery:
<h4 id="title">Lotus</h4>
to include script on top:
在顶部包含脚本:
<script>[[jQuery .js File Path]]</script>
On document ready do like this:
在准备好文档时,请执行以下操作:
$(document).ready(){
var text=$('#title').html();
});
but I would recommend to use id.
但我建议使用 id。
回答by user123_456
You can use jQuery for that :
您可以为此使用 jQuery:
<h4 id="title">Lotus</h4>
to include script on top:
在顶部包含脚本:
<script>[[jQuery .js File Path]]</script>
On document ready do like this:
在准备好文档时,请执行以下操作:
$(document).ready(){
var text=$('#title').html();
});
but I would recommend to use id's
or classes
但我建议使用id's
或classes
回答by hr_117
You can use "innerHTML" in your example.
您可以在示例中使用“innerHTML”。
var text = h.getElementsByTagName("h4")[0].innerHTML;
Or "innerText"
或“内文”
var text = h.getElementsByTagName("h4")[0].innerText
回答by Ivailo Karamanolev
I highly recommend you use jQueryfor this type of work. With jQuery, it's pretty easy:
我强烈建议您将jQuery用于此类工作。使用 jQuery,这很容易:
var i = $('h4').text();
However, whenever you can - use the id attribute.
但是,只要可以 - 使用 id 属性。
<h4 id="doc_head">Lotus</h4>
Then in JS:
然后在JS中:
var i = $('#doc_head').text();
If you don't want to use jQuery for some reason - get the text like that (ugly):
如果您出于某种原因不想使用 jQuery - 获取这样的文本(丑陋):
var i = h.textContent || h.innerText; // having your DOM object in h
The above should work in both IE and W3C compliant browsers, look here - InnerText alternative in mozilla
以上应该适用于 IE 和 W3C 兼容的浏览器,看这里 - Mozilla 中的 InnerText 替代