简体   繁体   中英

how to get the node html passing a specific tag name

I have many nodes, i want to get this node when data-th is "Entrega"

<span class=“price” data-bind=“text: getValue(), attr: {‘data-th’: title}” data-th=“Entrega”>0.00</span>

is it possible?

Yes. Just use querySelector to match that data attribute value.

Note: your HTML had smart quotes in it (perhaps from copying) but they will cause issues with your code functioning.

 const th = document.querySelector('span[data-th="Entrega"]'); console.log(th); 
 <span class="price" data-bind="text: getValue(), attr: {'data-th': title}" data-th="Entrega">0.00</span> 

The easiest way would be to loop throw all those spans and get the one you need.

object.getAttribute('data-th').includes('Entrega')

The code above gets the data attribute data-th from the object and check if it contains the string 'Entrega'.

For more information about data-attributes check:

https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

 function callFunction(){ // GET ALL THE SPANS var spandata = document.getElementsByTagName('span'); // LOOP ON THAT Array.from(spandata).forEach(function(object){ // THESE PRINT THE VALUE OF data-th console.log(object.getAttribute('data-th')); // IF ITS Entrega THEN DO SOMETHING if(object.getAttribute('data-th').includes('Entrega')){ // DO SOMETHING HERE alert(object.getAttribute('data-th')); } }) } 
 span{ display:block; } 
 <span class=“price” data-bind=“text: getValue(), attr: {'data-th': title}” data-th=“Entrega”>0.00</span> <span class=“price” data-bind=“text: getValue(), attr: {'data-th': title}” data-th=“Papa”>1.00</span> <span class=“price” data-bind=“text: getValue(), attr: {'data-th': title}” data-th=“Roach”>2.00</span> <button onclick="callFunction()">print here</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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