简体   繁体   English

带有脚本标签的jquery选项卡中的动态内容

[英]dynamic content in jquery tabs with script tags

I am trying to load dynamic content in each of the four jquery tabs. 我试图在四个jquery选项卡的每个中加载动态内容。 Whenever user click on tab, I request a jquery ajax call to get the content for that clicked tab. 每当用户单击选项卡时,我都会请求jquery ajax调用以获取该单击的选项卡的内容。 The content is a set of accordions with some script tags in it. 内容是一组带有一些脚本标签的手风琴。 When I do a console.log in chrome, I can see all the script tags. 当我在chrome中执行console.log时,我可以看到所有脚本标签。 But as soon as I assign the content to the tab content like this 但是一旦我将内容分配给这样的标签内容

First I bind the tabs like below 首先,我将标签绑定如下

$(".tabs").tabs({
              activate: function (event, ui) {
                  var active = $('.tabs').tabs('option', 'active');
                  console.log(active);
                  $(".tabs-" + active).html('test content');

                  var tab_text = $(".tabs ul>li a").eq(active).html();
                  var GroupCode;
                  if (tab_text == "Excel 2003")
                      GroupCode = "2003";
                  else if (tab_text == "Excel 2007")
                      GroupCode = "2007";
                  else if (tab_text == "Excel 2010")
                      GroupCode = "2010";
                  else if (tab_text == "Excel 2013")
                      GroupCode = "2013";


                  var tab_content;
                  tab_content = GetMyData(GroupCode);
                  //console.log(tab_content);
                  $('.ui-tabs-panel').not('.ui-tabs-hide').html(tab_content);

              }
          });

when the particular tab is activated, I called a method to get the content through jquery ajax GetMyData(GroupCode) 当特定的选项卡被激活时,我调用了一种方法来通过jquery ajax GetMyData(GroupCode)获取内容

function GetMyData(groupCode) {
         groupCode = groupCode.replace("CGRO","");
          var _optionList;              

          $.ajax({
              type: "POST",
              url: "getcontent.aspx?cg=" + groupCode,
              data: {},
              async: false,
              dataType: "html",
              success: function (response) {

                  _optionList = response;
                  $(".accordion").accordion();
              },
              failure: function (response) {
                  alert(response.d);
              },
              error: function (jq, status, message) {
                  alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
              }
          });

         // console.log(_optionList);
          return _optionList;
      }

and that's the line where i'm passing content to the active tab 这就是我将内容传递到活动选项卡的那一行

 $('.ui-tabs-panel').not('.ui-tabs-hide').html(tab_content);

tab_content has everything include script tab, but after passing it to .html, i cant see any script tag that were in tab_content. tab_content具有包括脚本选项卡在内的所有内容,但是将其传递给.html之后,我看不到tab_content中的任何脚本标记。

I googled for few days and can see that one option is innerHTML, but in my case that's not working on tabs. 我在Google上搜索了几天,可以看到一个选项是innerHTML,但就我而言,这在选项卡上不起作用。 I also tried .text, but that didnt render the html, instead just spit out content in plain text. 我还尝试了.text,但是没有呈现html,而是只将内容吐出为纯文本。

How can I load content from ajax call that has script tags in it. 如何从包含脚本标签的ajax调用中加载内容。

NOTE: I am using asp.net(VB) to load the content which has html and javascript. 注意:我正在使用asp.net(VB)加载具有html和javascript的内容。 Thank you in advance. 先感谢您。

Try utilizing jquery $.parseHTML() , escaping closing script tags , ieg, <\\/script> 尝试利用jquery $ .parseHTML() ,转义结束script标签,例如, <\\/script>

$(function() {
var str = "<script type=text/javascript>alert(123)<\/script>"
             + "<span>abc</span>";
var script = $.parseHTML(str, document, true);
    $("body").append($("<div>"));
    $("div").html(script);
});

jsfiddle http://jsfiddle.net/guest271314/uH42S/ jsfiddle http://jsfiddle.net/guest271314/uH42S/

The problem is that function GetMyData makes an AJAX ( Asynchronous JavaScript And XML) call. 问题在于函数GetMyData进行了AJAX( 异步 JavaScript和XML)调用。

tab_content = GetMyData(GroupCode); // <-- hidden AJAX call
//console.log(tab_content); // <-- will always be undefined
$('.ui-tabs-panel').not('.ui-tabs-hide').html(tab_content); // <-- appends undefined

The reason tab_content will always be undefined is that the call to GetMyData schedules the AJAX call and then continues executing the activate function. tab_content始终是不确定的,原因是对GetMyData的调用会安排AJAX调用,然后继续执行activate函数。 Since the AJAX call has not been called yet, it cannot return _optionList so the tab_content variable remains undefined. 由于尚未调用AJAX调用,因此它无法返回_optionList因此tab_content变量保持未定义状态。

After the activate function has finished executing, the AJAX call is made and _optionList is eventually returned, but since the activate function has finished executing the return is pointless (and ignored). activate函数完成执行之后,将进行AJAX调用并最终返回_optionList ,但是由于activate函数完成执行后, return毫无意义(并且被忽略)。

The solution is to pass along a reference to your container, or a callback that will fill the container, when the AJAX call has finished: 解决方案是在AJAX调用完成时传递对容器的引用或将填充容器的回调:

function GetMyData(groupCode, container) { // passing reference to container
    $.ajax({
        // ... other options ....
        "success": function (response) {
            container.html(response);
            // _optionList = response; // <-- not needed unless you want to store the response somewhere, like in a data attribute.
            $(".accordion").accordion();
        },
        // ... other options ....
    });
}

// call like this

GetMyData(GroupCode, $('.ui-tabs-panel').not('.ui-tabs-hide'));

Another option is: 另一个选择是:

function GetMyData(groupCode, callback) { // passing callback
    $.ajax({
        // ... other options ....
        "success": function (response) {
            callback(response);
        },
        // ... other options ....
    });
}

// call like this

GetMyData(GroupCode, function (tab_content) {
    $('.ui-tabs-panel').not('.ui-tabs-hide').html(tab_content);
    $(".accordion").accordion();
});

EDIT 编辑

I just saw where you are setting async to false which pretty much invalidates my answer. 我刚刚看到您在哪里将async设置为false ,这几乎使我的答案无效。 Please follow the code below to help us help you debug this. 请按照下面的代码来帮助我们调试。

function GetMyData(groupCode) {
    var _optionList;
    groupCode = groupCode.replace('CGRO', '');
    $.ajax({
        "type": "POST",
        "url": "getcontent.aspx?cg=" + groupCode,
        "data": {},
        "async": false,
        "dataType": "html",
        "success": function (response) {
            console.log(response); // <-- Add this line exactly here and post the log in your question.
            _optionList = response;
            $('.accordion').accordion();
        },
        "failure": function (response) {
            alert(response.d);
        },
        "error": function (jq, status, message) {
            alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
        }
    });
    // console.log(_optionList);
    return _optionList;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM