使用javascript检索标签数据

时间:2020-12-05 01:38:50

I have this <noscript> tag data I want to get retrieve image src attribute data by javascript / jQuery I have tried too much but i can't get please help me to get image src attribute data

我有这个

<script>
// Using javascript 
var nos = document.getElementsByTagName("noscript")[0];
var htmlData = nos.innerHTML;
</script>

jQuery(function() {
var data = jQuery("noscript").closest("#anaImg").attr('src');
      alert(data);     
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<noscript>
<p><img src="http://xxxx.xxxxx.com/analytics/conv_tracking.php?idsite=1545" style="border:0;" alt="" id="anaImg"/></p>
</noscript>

1 个解决方案

#1


2  

You can use a hidden div and append the noscript tag content in it as html

您可以使用隐藏的div并将其中的noscript标记内容附加为html

Example:

var noscript_text = $("noscript").text();     //get noscript tag content
$('#noscript').append($(noscript_text));      //append to another div to get the image src
var get_img_src = $('#anaImg').attr('src');   // get the img src
var new_img_src = "nothing";                  // type your new img src here
var replace_img_src = noscript_text.replace(get_img_src,new_img_src); // replace old img src with new one
$("noscript").text(replace_img_src);          // change the content of no script tag with new src
alert($("noscript").text());                  // alert the new noscript tag content
#noscript{
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<noscript>
<p><img src="http://xxxxxxxx.xxxxxxxxxxx.com/analytics/conv_tracking.php?idsite=1545" style="border:0;" alt="" id="anaImg"/></p>
</noscript>

<div id="noscript"></div>

Note: please take care from #noscript this is an ID for the div and noscript this is for noscript tag

注意:请注意#noscript这是div和noscript的ID,这是noscript标签

#1


2  

You can use a hidden div and append the noscript tag content in it as html

您可以使用隐藏的div并将其中的noscript标记内容附加为html

Example:

var noscript_text = $("noscript").text();     //get noscript tag content
$('#noscript').append($(noscript_text));      //append to another div to get the image src
var get_img_src = $('#anaImg').attr('src');   // get the img src
var new_img_src = "nothing";                  // type your new img src here
var replace_img_src = noscript_text.replace(get_img_src,new_img_src); // replace old img src with new one
$("noscript").text(replace_img_src);          // change the content of no script tag with new src
alert($("noscript").text());                  // alert the new noscript tag content
#noscript{
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<noscript>
<p><img src="http://xxxxxxxx.xxxxxxxxxxx.com/analytics/conv_tracking.php?idsite=1545" style="border:0;" alt="" id="anaImg"/></p>
</noscript>

<div id="noscript"></div>

Note: please take care from #noscript this is an ID for the div and noscript this is for noscript tag

注意:请注意#noscript这是div和noscript的ID,这是noscript标签