2015-07-28 22 views
0

在CMS环境中实现此代码时。获取beloe错误。AJAX调用outerHTML uncaught typeError

遗漏的类型错误:无法未定义

谁能帮我的读取属性“outerHTML”来解决这个问题?

在此先感谢

main.html中:

<div class="pwrapper"> 
        <select name="country" id="scountry"> 
        <option id="EN" value="global.html">GLOBAL, ENGLISH</option> 
        <option id="AU" value="australia.html">AUSTRALIA</option> 
        </select> 
       </div> 
       <script type="text/javascript"> 
        $(document).ready(function() {      
         $('.country #scountry').on('change', function(e){ 
          var ajaxURL = $(this).val(); 
          $.ajax({ 
           url: ajaxURL, 
           success: function(data) { 
            var htmlData = $(data); 
            $('#scontainer').html(htmlData.filter('#ajax-container').get(0).outerHTML); 
           } 
          });       
         });       
         $('.countrySelector #scountry').trigger('change'); 
        }); 
       </script> 
</div> 

<div class="wrapper"> 
<section class="no-padding"> 
    <div id="scontainer"> 
    </div> 
</section> 
</div> 

global.html(在main.html中的子页面):

<div id="ajax-container"> 
    Hello World!! 
</div> 
+0

不'$(“#scontainer”)HTML (数据);'工作? – lshettyl

+0

你不需要outerHTML ...只需插入jQuery对象。如果它是返回的根的唯一元素...插入'data'并且不需要过滤 – charlietfl

回答

0

替换此行:

htmlData.filter('#ajax-container').get(0).outerHTML 

与此代码

htmlData.find('#ajax-container').html(); 
+0

而不是:htmlData.filter('#ajax-container')。get(0).outerHTML – Alexander

+0

太棒了。 .. 有用!!! – TDG

0

这是使用load()其是用于$.ajax的快捷方法的理想情况。网址是用来确定在容器中插入该请求的HTML响应的片段后

$('#scontainer').load(ajaxURL +' #ajax-container); 

空间分隔选择:

你可以替换当前$.ajax请求,并与回调。如果返回的唯一的根元素是你想要的ID,你不需要对任何碎片过滤和可以删除片段选择

参考:load() docs

相关问题