您好我从XMLHttpRequest获取了一个JSON字符串,我将其转换为JavaScript对象:
response = JSON.parse(xmlhttp.responseText);
其中一个JSON元素值是一个字符串,表示带有noscript标记的脚本标记(输出显示console.log(response.ad.con);):
<SCRIPT language='JavaScript1.1' SRC="https://ad.doubleclick.net/ddm/adj/N378.150781.4704472308521/B5632202.128522416;sz=300x250;ord=[timestamp];dc_lat=;dc_rdid=;tag_for_child_directed_treatment=?"></SCRIPT><NOSCRIPT><A HREF="https://ad.doubleclick.net/ddm/jump/N378.150781.4704472308521/B5632202.128522416;sz=300x250;ord=[timestamp]?"><IMG SRC="https://ad.doubleclick.net/ddm/ad/N378.150781.4704472308521/B5632202.128522416;sz=300x250;ord=timestamp];dc_lat=;dc_rdid=;tag_for_child_directed_treatment=?" BORDER=0 WIDTH=300 HEIGHT=250 ALT="Advertisement"></A></NOSCRIPT>
我现在想要获取脚本标记的SRC元素中的字符串。 我怎样才能做到这一点?我想我在转换中迷失了......
非常感谢!
答案 0 :(得分:2)
您可以使用DOMParser来解析HTML字符串,就像JSON等一样,然后只使用DOM方法获取属性
var response = JSON.parse(xmlhttp.responseText);
var parser = new DOMParser();
var doc = parser.parseFromString(response.propertyWithHtml, "text/html");
var src = doc.querySelector('script').getAttribute('src');
答案 1 :(得分:1)
如果你使用jQuery, 试试这个
$('<SCRIPT language="JavaScript1.1" SRC="https://ad.doubleclick.net/ddm/adj/N378.150781.4704472308521/B5632202.128522416;sz=300x250;ord=[timestamp];dc_lat=;dc_rdid=;tag_for_child_directed_treatment=?"></SCRIPT><NOSCRIPT><A HREF="https://ad.doubleclick.net/ddm/jump/N378.150781.4704472308521/B5632202.128522416;sz=300x250;ord=[timestamp]?"><IMG SRC="https://ad.doubleclick.net/ddm/ad/N378.150781.4704472308521/B5632202.128522416;sz=300x250;ord=timestamp];dc_lat=;dc_rdid=;tag_for_child_directed_treatment=?" BORDER=0 WIDTH=300 HEIGHT=250 ALT="Advertisement"></A></NOSCRIPT>').attr("SRC")