[英]How to get element value from JSON object and first convert it to HTML and then to String
Hi i´m getting a JSON string from an XMLHttpRequest which I fist convert into a JavaScript Object: 嗨,我从XMLHttpRequest获取JSON字符串,然后我将其转换为JavaScript对象:
response = JSON.parse(xmlhttp.responseText);
One of the JSON element values is a string which represents a script tag with a noscript tag like that (output shows console.log(response.ad.con);): 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>
I now wanna get the string which is in the SRC element of the script tag. 我现在想获取脚本标记的SRC元素中的字符串。 How can I do that?
我怎样才能做到这一点? I think i´m lost in conversion...
我想我迷失了转换...
Thanks a lot! 非常感谢!
You can use DOMParser to parse HTML strings, just like you would JSON etc. and then just get the attribute with DOM methods 您可以使用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');
如果您使用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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.