通过jquery运行另一个html页面上的javascript代码
这可能是一个noob问题,但我刚刚开始使用jquery。 我的基本要求是提取另一个html中存在的javascript代码中的链接(代码嵌入在html页面中,而不是单独的文件中) 该链接还作为通过jquery运行另一个html页面上的javascript代码,javascript,jquery,Javascript,Jquery,这可能是一个noob问题,但我刚刚开始使用jquery。 我的基本要求是提取另一个html中存在的javascript代码中的链接(代码嵌入在html页面中,而不是单独的文件中) 该链接还作为 如果无法提取链接,是否可以运行该页面上的Java脚本?如果您能够通过.ajax成功获取页面的html代码,并且所需的数据在html代码中,那么不值得费心运行脚本。只需通过DOM访问URL: // ajax success function success: function(html) { va
如果无法提取链接,是否可以运行该页面上的Java脚本?如果您能够通过
.ajax
成功获取页面的html代码,并且所需的数据在html代码中,那么不值得费心运行脚本。只需通过DOM访问URL:
// ajax success function
success: function(html) {
var anchorCode = $(html)
// this assumes that noscript is a top-level element
// otherwise, use .find('noscript')
.filter('noscript')
.text(); // get the code for the anchor tag, as a string
var myLink = $(anchorCode).attr('href');
// do something with myLink
}
Edit:事实证明,jQuery处理
noscript
标记的方式有点滑稽-内部标记似乎不被视为DOM的一部分,因此您需要获取标记的文本内容,然后使用jQuery对其进行DOM化。请参阅上面的更新代码。我不理解您的问题。你能试着重写一下吗?例如,我想从另一个人的网站检索
标记中的javascript。然后我想在我的网站上运行这个javascript。按要求编辑。让我知道,如果你仍然没有得到我的问题,我只想提取一个链接,这是一个javascript代码中存在的。还可以运行该页面上的javascript吗?当我执行alert(myLink)检查页面中的内容时,我得到了“undefined”(未定义),您确定您正确获得了HTML代码,并且它包括了您上面显示的noscript
标记吗?(顺便说一句,我强烈建议使用Firebug或Chrome控制台等控制台进行调试,而不是alert()
调用)
<noscript>
<a href = "a link I want to extract comes here" target="_blank">blabla</a>
</noscript>
// ajax success function
success: function(html) {
var anchorCode = $(html)
// this assumes that noscript is a top-level element
// otherwise, use .find('noscript')
.filter('noscript')
.text(); // get the code for the anchor tag, as a string
var myLink = $(anchorCode).attr('href');
// do something with myLink
}