尝试从jQuery ajax获取响应中选择脚本标签

前端之家收集整理的这篇文章主要介绍了尝试从jQuery ajax获取响应中选择脚本标签前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在第A页。点击链接,我通过jQuery从页面B加载到DOM中。页面B的DOM是多个动态生成的脚本标签,类“dataScript”以及一堆其他脚本标签,我不想要任何事情。

我想要的DOM的唯一的东西是.dataScript标签,然后我想要插入一个ID为“scriptOutput”的div到页面A的DOM中。如果类“ dataScript“是脚本标签。只有它是一些其他标签,如“div”标签。这是我正在尝试做的一个例子:

页面A:

<html>
<head>
<title>Page A</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function() {
 $("#ajaxJsLink").click(function() {
  $.get("pageB.html",function(data) {
   var scriptElements = $(data).find(".dataScript").contents();
   console.log(scriptElements);
   $(scriptElements).each(function(index) {
    $("#scriptOutput").append($(this).html());
   });
  });
  return false;
 });
 $("#ajaxDivsLink").click(function() {
  $.get("pageB.html",function(data) {
   var scriptElements = $(data).find(".dataDiv").contents();
   console.log(scriptElements);
   $(scriptElements).each(function(index) {
    $("#divOutput").append($(this).html());
   });
  });
  return false;
 });
});
</script>
</head>
<body>
<p>This is page A.</p>
<hr />
<p>
<a href="pageB.html" id="ajaxJsLink">Get JavaScript from Page B.</a><br />
<a href="pageB.html" id="ajaxDivsLink">Get Divs from Page B.</a>
</p>
<hr />
<div id="scriptOutput">
 <h2>Script Output</h2>
</div>
<div id="divOutput">
 <h2>Div Output</h2>
</div>
</body>
</html>

页面B:

<html>
<head>
<title>Page B</title>
</head>
<body>
<p>This is page B.</p>
<div id="scripts">
 <script type="text/javascript" class="dataScript">
  function someFunction() {
   alert("I am");
  }
 </script>
 <script type="text/javascript" class="dataScript">
  function anotherFunction() {
   alert("Javascript");
  }
 </script>
</div>
<div id="divs">
 <div class="dataDiv">
  <div>
   function someFunction() {
    alert("I am");
   }
  </div>
 </div>
 <div class="dataDiv">
  <div>
   function anotherFunction() {
    alert("Html");
   }
  </div>
 </div>
</div>
</body>
</html>

我已经尝试为.dataScript内容添加.contents(),.html()和.text(),但似乎没有任何效果。感谢您的看法/回答我的问题。我感谢您的帮助!

更新:

如果其他人试图这样做,这里是我最终的不太优雅但功能齐全的解决方案:

输出javascript作为普通文本(没有脚本标签)在一个div内(一个ID,并设置为显示:none)在页面B.然后在页面A上,在get请求的回调函数中执行以下操作:

var docHead = document.getElementsByTagName("head")[0]; //head of Page A
var newScript = document.createElement("script");
newScript.setAttribute("type","text/javascript");
newScript.innerHTML = jQuery(data).find("#divWithPlainTextJs").text(); //insert plain text JS into script element
docHead.appendChild(newScript); //append script element to head of Page A
jQuery("#divWithPlainTextJs").remove(); //remove the plain text div and JS from the DOM

感谢Emmett提醒我的document.createElement方法

解决方法

jQuery实际上不附加< script>元素到DOM。相反,它只是废除了脚本的内容。因为它不在DOM中,所以$(data).find(“。dataScript”)不匹配任何东西。

如果您真的需要< script>的内容标签,您可以尝试使用正则表达式来解析ajax响应。

查看Karl Swedberg’s comment了解更多信息:

All of jQuery’s insertion methods use
a domManip function internally to
clean/process elements before and
after they are inserted into the DOM.
One of the things the domManip
function does is pull out any script
elements about to be inserted and run
them through an “evalScript routine”
rather than inject them with the rest
of the DOM fragment. It inserts the
scripts separately,evaluates them,
and then removes them from the DOM.

I believe that one of the reasons jQuery does this is to avoid “Permission Denied” errors that can occur in Internet Explorer when inserting scripts under certain circumstances. It also avoids repeatedly inserting/evaluating the same script (which could potentially cause problems) if it is within a containing element that you are inserting and then moving around the DOM.

原文链接:https://www.f2er.com/jquery/181862.html

猜你在找的jQuery相关文章