开发者

Ajax returns HTML child template - insert in DIV fails

开发者 https://www.devze.com 2023-03-13 03:19 出处:网络
The following script gets HTML from AJAX call - HTML displays fine by itself in browser - I use InnerHtml to put it within DIV, I get javascript error :

The following script gets HTML from AJAX call - HTML displays fine by itself in browser - I use InnerHtml to put it within DIV, I get javascript error :

Uncaught TypeError: Cannot call method 'setData' of undefined

I have put the same exact HTML by hard-coding inside the template, it works fine - it just doesn't work when I insert it in DIV dynamically! any ideas?

<script> 
  $(document).ready(function() 
  { $('ul.art-vmenu li').cli开发者_开发知识库ck(function(e) 
     { 
       //Ajax call to get content:
       $.ajax(
       {
             type: "GET",
             url: "/create",
             data: "",
             success: function(msg)
             {
               //alert(msg);   
               document.getElementById("art-post-inner art-article").innerHtml = msg; 
               //$("#art-post-inner art-article").html(msg);  // jQuery call             
               window.clipboardData.setData("Text", msg);  // for debug in IE               
             }
       });

     });
  });    
</script>   


first of all: it's innerHTML, not innerHtml

second: which browser are you using? clipboardData is only available in IE

check also: How do I copy to the clipboard in JavaScript?


You're trying to reference innerHTML on an undefined value, because document.getElementById is returning undefined, because this code is asking for an invalid ID:

document.getElementById("art-post-inner art-article").innerHtml = msg; 
//            Spaces not allowed ------^     and      ^----- Should be innerHTML

(+1 to roberkules for pointing out the innerHTML thing, I read right past it.)

Rules for IDs vary depending on whether you're using HTML4 and below, HTML5 (which is much more permissive), or CSS (restrictive), but one thing they all have in common is that you cannot have a space in an ID:

  • Rules for id values - HTML4
  • Rules for id values - HTML5
  • Rules for id values - CSS

My guess is that that isn't an ID, that you intend it to be a selector of some kind, but as you haven't shown any markup I can't help with indicating how you might fix it other than to say that if it's a selector, you don't want document.getElementById, you want to use jQuery's $ as you have elsewhere in your code.


You are getting "Uncaught TypeError: Cannot call method 'setData' of undefined" error because window.clipboardData is undefined. innerHtml is also wrong.

You should always use jquery html method to set the html to any dom element because jquery takes care of executing any script tags withing the markup which you are setting.

0

精彩评论

暂无评论...
验证码 换一张
取 消