jquery:保持< script> .find()之后的标签 [英] jquery: Keep <script> tag after .find()

查看:75
本文介绍了jquery:保持< script> .find()之后的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过jquery-ajaxy插件向WordPress添加ajax,而且我遇到了一件事:

I'm trying to add ajax to WordPress by jquery-ajaxy plugin, and I'm stuck with one thing:

我的页面是由ajax调用加载的,过滤,并附加到dom。简而言之,它将是:

My page is loaded by ajax call, filtered, and appended to dom. So in short, it would be:

data = this.state.Response.data; //full html of page returned by ajax
$mainContent = $(data).find("#content"); //we filter out what we need 
$('#content_div').append($mainContent); //we display it

现在很简单,但$ mainContent中有内联js脚本,由jquery条纹化。
不知怎的,我需要它们。

Easy for now, but there are inline js scripts in $mainContent, which are striped by jquery. I need them, somehow.

一切正常,如果我这样做

It all works, if I just do

.append(data);  

但数据包含完整的html(doctype,head,meta),我无法附加。
那么,有没有办法让这些标签在 .find()之后工作?

But data contains full html (doctype, head, meta) which I can't append. So, is there a way to make those tags work after .find()?

推荐答案

每当从字符串创建jQuery对象时,都会自动删除脚本标记。您可以在此处查看讨论:用于div标签的JavaScript RegEx

Whenever you create a jQuery object from a string, script tags are automatically stripped. You can see a discussion in here: JavaScript RegEx for div tags

根据上面帖子中的帖子的建议,我会做的是将内容包装在特殊注释标签中并使用javascript match()提取它,追加整个字符串。

What I would do, as suggested in a post in the thread above, is to wrap the content in special comment tags and use javascript match() to extract it, the append the whole string.

data = this.state.Response.data; //full html of page returned by ajax

//Assuming your data looks like: ...<!--Content Start-->CONTENT<!--Content End-->...
//we filter out what we need 
data = data.match(/<\!\-\-Content Start\-\->(.*?)<\!\-\-Content End\-\->/)[1];

$('#content_div').append(data); //we display it

这篇关于jquery:保持&lt; script&gt; .find()之后的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆