javascript - 使用 javascript 检索 <noscript> 标签数据

标签 javascript jquery noscript

我有这个<noscript>我想通过 javascript/jQuery 获取图像 src 属性数据的标签数据 我已经尝试了太多但我无法获取请帮助我获取图像 src 属性数据

<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>

最佳答案

你可以使用一个隐藏的div,并在其中附加noscript标签内容作为html

示例:

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

关于javascript - 使用 javascript 检索 <noscript> 标签数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43535957/

相关文章:

javascript - 如何将通过 AJAX 接收到的数据传递到 ASP MVC 部分 View ?

javascript - 如何使用 noscript 标签?

google-tag-manager - GTM 的 Noscript 标签 - 是必需的吗?

jquery - Jquery中如何检查字符串是否比容器div的宽度长?

javascript - 组合 jquery 代码块

Jquery 将父级替换为第一个子级

php - 使用无脚本重新填充表单和显示错误的最佳方法是什么?

javascript - 为什么我的数组项被添加到顶部,而滚动条仍保留在顶部?

javascript - Safari 中的 CSS 缩放问题

javascript - Heroku 部署错误 : Cannot get Node App running after Deploy : Cannot find module '/app/web.js'