最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript get the value from an h4 Tag - Stack Overflow

programmeradmin0浏览0评论

i am trying to get the value Lotus of the h4 tag from the code below

    <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&nbsp;1 mai</strong>&nbsp;9h</em> au <em><strong>dimanche&nbsp;5 mai</strong>&nbsp;6h</em> </p>

    <p class="baseline"></p>

    </a>    

to do that i did as follow :

var h=document.getElementById("17285");

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.

i am trying to get the value Lotus of the h4 tag from the code below

    <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&nbsp;1 mai</strong>&nbsp;9h</em> au <em><strong>dimanche&nbsp;5 mai</strong>&nbsp;6h</em> </p>

    <p class="baseline"></p>

    </a>    

to do that i did as follow :

var h=document.getElementById("17285");

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.

Share Improve this question edited Sep 14, 2018 at 10:18 gic186 8361 gold badge7 silver badges19 bronze badges asked May 4, 2013 at 10:36 SamSam 4874 gold badges12 silver badges32 bronze badges 3
  • did you looked here: stackoverflow./questions/5321739/… – Ivan Chernykh Commented May 4, 2013 at 10:42
  • BTW: id's must not be numbers only, they have to start with a character at least – Fabian Lauer Commented May 4, 2013 at 10:43
  • Numbers are allowed in html5 – mplungjan Commented May 4, 2013 at 10:45
Add a ment  | 

5 Answers 5

Reset to default 2

Any of these will work - I remend the last

console.log(
  document.getElementById("linkSale").getElementsByTagName("h4")[0].textContent,
  document.getElementById("linkSale").querySelector("h4").textContent,
  document.querySelector("#linkSale h4").textContent
)
<a id="linkSale" class="linkAccess" title="" href="/vp4/Home/Handlers/OperationAccess.ashx?operationid=17285">
  <h4>Lotus</h4>
  <p class="dateSales">Du <em><strong>mercredi&nbsp;1 mai</strong>&nbsp;9h</em> au <em><strong>dimanche&nbsp;5 mai</strong>&nbsp;6h</em> </p>
  <p class="baseline"></p>
</a>

you can use jQuery for that :

<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 remend to use id.

You can use "innerHTML" in your example.

 var text = h.getElementsByTagName("h4")[0].innerHTML;

Or "innerText"

var text = h.getElementsByTagName("h4")[0].innerText

I highly remend you use jQuery for this type of work. With jQuery, it's pretty easy:

var i = $('h4').text();

However, whenever you can - use the id attribute.

<h4 id="doc_head">Lotus</h4>

Then in JS:

var i = $('#doc_head').text();

If you don't want to use jQuery for some reason - get the text like that (ugly):

var i = h.textContent || h.innerText; // having your DOM object in h

The above should work in both IE and W3C pliant browsers, look here - InnerText alternative in mozilla

You can use jQuery for that :

<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 remend to use id's or classes

发布评论

评论列表(0)

  1. 暂无评论