jQuery在HTML字符串中评估JavaScript并发症(jQuery evaluating JavaScript in HTML string with complications)

编程入门 行业动态 更新时间:2024-10-27 17:23:47
jQuery在HTML字符串中评估JavaScript并发症(jQuery evaluating JavaScript in HTML string with complications)

这真让我抓狂...

我要做的是更新网页的一部分,由于不值得解释的原因,请求是要求一个全新的HTML页面(包括其头部),然后只提取指定元素中包含的HTML (由身份证明)。

最初,我有以下JavaScript代码,只要完成对新HTML的AJAX请求,它就会运行:

var $newContent = $(newHtmlContent); loadingElement.innerHTML = $("#" + loadingElementId, $newContent).html();

这很棒,直到我加载了一些HTML,包含在指定的元素中,包含了一些需要运行的脚本,所以我把它更改为:

var $newContent = $(newHtmlContent); $(loadingElement).html($("#" + loadingElementId, $newContent).html());

我已经读过jQuery将使用html()函数的HTML字符串参数来评估任何脚本 - 但是,似乎问题是在调用之前脚本被剥离了。 通过firebug看了以下似乎正在发生:

$(newHtmlContent) // includes all 'script' elements $("#" + loadingElementId, $newContent) // has no scripts $("script", $newContent) // empty jQuery object

有没有人有什么建议? 提前致谢。

This is driving me crazy...

What I am trying to do is update a portion of a webpage, and for reasons not worth explaining, the request is asking for a whole new HTML page (including its head) and then pulling out only the HTML that was contained within a specified element (identified by an ID).

Originally, I had the following JavaScript code that would run whenever an AJAX request for new HTML completed:

var $newContent = $(newHtmlContent); loadingElement.innerHTML = $("#" + loadingElementId, $newContent).html();

This was great until I had a bit of HTML that was loaded that, contained within the specified element, included some scripts that needed to be run, so I changed it to this:

var $newContent = $(newHtmlContent); $(loadingElement).html($("#" + loadingElementId, $newContent).html());

I have read that jQuery will evaluate any scripts with the HTML string argument of the html() function - however, it seems the issue is that the scripts are stripped before this call. Having had a look through firebug the following seems to be happening:

$(newHtmlContent) // includes all 'script' elements $("#" + loadingElementId, $newContent) // has no scripts $("script", $newContent) // empty jQuery object

Does anyone have any suggestions? Thanks in advance.

最满意答案

快速测试(小提琴) ,给出以下标记为字符串:

<div id="root"> <div>some dynamic content</div> <script type="text/javascript">alert("hello");</script> <div class="desiredElement">after script</div> </div>

jQuery会生成一个像这样的对象

> [0]: #root > [1]: script

[0]将包含#root除脚本之外的所有html,脚本被分成各自的对象。 这似乎适用于标记中的所有脚本。

所以你可以做这样的事情来运行脚本:

var $new = $(dynamicContent); // dynamicContent is string var $desired = $new.find('.desiredElement'); $new.eq(1).appendTo('body'); // run the script

Did a quick test (fiddle), given the following markup as string:

<div id="root"> <div>some dynamic content</div> <script type="text/javascript">alert("hello");</script> <div class="desiredElement">after script</div> </div>

jQuery will produce an object like

> [0]: #root > [1]: script

[0] will contain all of the html in #root except for the script, the scripts are separated into their own objects. This seems to be true for all scripts within the markup.

So you could do something like this to run the script:

var $new = $(dynamicContent); // dynamicContent is string var $desired = $new.find('.desiredElement'); $new.eq(1).appendTo('body'); // run the script

更多推荐

本文发布于:2023-08-04 15:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并发症   字符串   jQuery   HTML   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!