相关疑难解决方法(0)

尝试从jQuery ajax get响应中选择脚本标记

我在页面A上.单击了一个链接,我通过jQuery get从页面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 …
Run Code Online (Sandbox Code Playgroud)

jquery

16
推荐指数
1
解决办法
2万
查看次数

标签 统计

jquery ×1