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

查看:73
本文介绍了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中的元素.我有一个执行html生成工作的php文件,并将所有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号看到应该通过动态分配的选择器写入每个div的jquery函数.

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和tag,但是grrrrrr ...我仍然无法获得dam damn脚本标签显示在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 删除脚本标签(

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和tag,但是grrrrrr ...我仍然无法获得dem damn脚本标签显示在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天全站免登陆