jquery load() 去除脚本标签 - 解决方法? [英] jquery load() strips script tags - workaround?

查看:20
本文介绍了jquery load() 去除脚本标签 - 解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道 jquery .load() 去除从外部内容加载的脚本标签的解决方法?

Does anyone know of a work around for jquery .load() stripping out the script tags loaded from external content?

有很多文档证明会发生这种情况,但是在网上搜索了大约 4 小时后我找不到解决方法?

There's a lot of documentation of the fact that this happens but after something like 4 hours searching the net I can't find a work around?

我正在加载动态生成的 div - 类似于搜索结果页面 - 并且需要将 .click() 绑定到每个动态生成的 div 中的元素.我有一个 php 文件,它执行 html 生成工作并将所有 html 作为字符串返回,但是我无法绑定 jquery .click(),因为包含 jquery 函数的脚本标签被删除了.

I'm loading dynamically generated divs - similar to a page of search results - and need to bind a .click() to an element within each dynamically generated div. I've a php file that does the html generation work and returns all the html as a string however I can't bind the jquery .click() as the script tags containing the jquery function get stripped out.

这是通过调用php文件加载外部内容的代码...

here's the code that loads the external content by calling the php file...

$("#titles_wrap").load("m_scripts/m_php/titles_loader.php", function(){
                                                                $..some code
                                                                });

这是生成 div 的 php 文件中的循环(这工作正常)...

and here's the loop from the php file that generates the divs (this works fine)...

$tag1='<script type="text/javascript">';
$tag2='</script>';

while($result_array = mysql_fetch_array($result)) {

            if($i2<=($titles_total)){

                $_SESSION['titles_string'] .= '<li id="listItem_'.$i2.'">

                                                <div id="titles_list_item">

                                                    <div id="titles_list_image_box" style="background-image: url(../../images/thumbs_test/'.$result_array[0].'); background-repeat: no-repeat; ">'.($i2+1).'</div>
                                                    <div id="title_php_loader"></div>
                                                        <div id="title_info_wrap">

                                                        <div id="title_current"><span class="title_current_grey" >current title: </span><br>'.$result_array[1].'
                                                            </div>

                                                                <div id="title_form_wrap">
                                                                    <span class="title_current_grey" >new title: </span><br><input name="title_input_'.$i2.'" id="title_input_'.$i2.'" type="text" class="title_input"></input>
                                                                    <div id="title_send" class="title_send_'.$i2.'">GO</div>
                                                                </div>

                                                            </div>


                                                    '.$tag1.'
                                                    $(".title_send_'.$i2.'").click(function(){$("#title_php_loader").load("m_scripts/m_php/title_sender.php")})
                                                    '.$tag2.'

                                                </div>
                                            </li>';
                $i2++;
                                }
            }

很抱歉,如果第二个代码块有点矫枉过正",请告诉我简化的摘录是否更有用.也就是说,您可以在 php 代码最后一行的第 8 行看到 jquery 函数,该函数应该使用动态分配的选择器写入每个 div.

Sorry if this second code block is a bit on the 'overkill' side - let me know if a simplified excerpt would be more useful. That said, you can see on the 8th from last line of the php code the jquery function that should get written into each div with a dynamically assigned selector.

当然,也可能是代码中存在其他错误,但我无法测试它,直到我可以让 .load() 停止破坏它!

Of course, it could be that there's other errors in the code however I'm not going to be able to test it until I can get .load() to stop trashing it!

如果有人找到了解决这个问题的方法——甚至是有限恩典的解决方法——太棒了!

If anyone's found a solution to this - or even a workaround of limited grace - brilliant!

提前致谢,

干杯,

斯科特

编辑 - 编辑 - 编辑 - 编辑 - 编辑 - 编辑 - 编辑 - 编辑

@T.J.克劳德@Frug

@T.J. Crowder @Frug

感谢您的帮助!

我刚刚仔细查看了您演示中涉及的页面,是的,我看到您已经成功了.看起来很神奇,因为它们在那里 - 那些脚本标签,还有很多人无法让它工作 - 不幸的是,我就是其中之一!

I've just had a good hard look at the pages involved in your demo and yes, I see you've got it working. Seems amazing cos there they are - those script tags and there are SO many people out there who can't get it to work - unfortunately I'm one of them!

我看到你的演示和我的代码情况之间的唯一区别是

The only differences I see between your demo and my code situation was

1) 开始脚本标签中没有类型声明,

1) no type declaration in the opening script tag,

2)您正在加载一个带有脚本标记的页面作为 DOM 的一部分,而我正在加载 php 字符串输出(我真的不认为这很重要,嗯?当它到达客户端时,一切是一样的,不是吗?)

2) you're loading a page with script tags as part of the DOM, whereas I was loading php string output (I really don't think this matters tho', eh? By the time it hits the client it all comes to the same thing, no?)

3),您的 .load 调用正在获取整个页面,而我的调用仅返回元素.从那以后,我将输出字符串更改为包括 all 和标签,但 grrrrrrr ......我仍然无法让该死的脚本标签显示在 DOM 中.

3), your .load call was fetching a whole page whereas mine was returning only elements. I've since changed the output string to include all , and tags but grrrrrr...I still can't get dem damn script tags to show up in the DOM.

有什么建议吗?有很多我不知道的东西,所以可能是任何东西!谢谢S

Any suggestions? There's loads I don't know about so could be anything! thanks S

推荐答案

jQuery load 去除脚本标签(example),并且您从 PHP 脚本返回的内容中没有脚本标签.有一些 JavaScript,但它没有正确嵌入到 script 标记中,因此浏览器无法识别它.

jQuery load doesn't strip out script tags (example), and there are no script tags in the content you're returning from your PHP script. There is some JavaScript, but it's not correctly embedded in script tags and so it's not being recognized by the browser.

如果你想要 JavaScript 在这个:

If you want the JavaScript in this:

...
'.$tag1.'
$(".title_send_'.$i2.'").click(function(){$("#title_php_loader").load("m_scripts/m_php/title_sender.php")})
'.$tag2.'
...

要被识别为 JavaScript,请将其放在 script 标记中.

to be recognized as JavaScript, put it in a script tag.

...
'.$tag1.'
<script>"$(".title_send_'.$i2.'").click(function(){$("#title_php_loader").load("m_scripts/m_php/title_sender.php")})</script>
'.$tag2.'
...

...或者更好的是,在内容末尾使用单个 script 标记来连接,或者在 load 函数.

...or better yet, use a single script tag at the end of the content to hook things up, or the callback on the load function.

更新:重新

1) 开始脚本标记中没有类型声明,

1) no type declaration in the opening script tag,

没关系,请参阅这个更新的示例.浏览器根据它始终应用的相同规则解释 script 标记.

Doesn't matter, see this updated example. The browser interprets the script tag according to the same rules it always applies.

2)您正在加载一个带有脚本标记的页面作为 DOM 的一部分,而我正在加载 php 字符串输出(我真的不认为这很重要,嗯?当它到达客户端时,一切是一样的,不是吗?)

2) you're loading a page with script tags as part of the DOM, whereas I was loading php string output (I really don't think this matters tho', eh? By the time it hits the client it all comes to the same thing, no?)

是的,一旦进入浏览器,一切都是一样的.

Right, it all comes to the same thing once it gets to the browser.

3),您的 .load 调用正在获取整个页面,而我的调用仅返回元素.从那以后,我将输出字符串更改为包含 all 和标签,但 grrrrrrr...我仍然无法让该死的脚本标签显示在 DOM 中.

3), your .load call was fetching a whole page whereas mine was returning only elements. I've since changed the output string to include all , and tags but grrrrrr...I still can't get dem damn script tags to show up in the DOM.

我的示例只是加载一个片段,而不是整个页面.

My example is just loading a snippet, not a full page.

我不知道为什么你的不工作.我想知道它是否与循环输出的多个脚本元素有关,或者您正在连接 click 处理程序,但 两者都不是.

I don't know why yours isn't working. I wondered if it related to multiple script elements being output in a loop, or the fact you were hooking up a click handler, but it's not either of those.

除了拿你的不工作的例子,一点一点地剥去层,直到你找到一个部分,当你删除它时,它就可以开始工作了.从根本上说,使用 load 使脚本无法运行并不是 (无论您发现有多少人认为是这种情况;世界很大,有很多几乎什么都想的人).你必须放弃这与 jQuery 的 load 函数对脚本做任何事情有关的想法;可能不是.

There's nothing for it but to take your non-working example and peel layers away bit by bit until you find the part that, when you remove it, lets it start working. Fundamentally, again, it's not that using load makes the scripts not work (regardless of how many people you find who think that's the case; it's a big world, there are lots of people who think nearly anything). You have to let go of the idea that this is related to jQuery's load function doing anything to the scripts; it probably isn't.

真正调试这种东西的关键是简化,简化,再简化.确保服务器为 ajax 调用返回的 HTML 看起来像您认为的那样(没有奇怪的引号或其他东西).尝试用静态页面来做.等等.

The key to debugging this kind of thing really is simplify, simplify, simplify. make sure the HTML being returned by the server for the ajax calls looks the way you think it does (no weird quotes or something). Try to do it with static pages. Etc.

这篇关于jquery load() 去除脚本标签 - 解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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