开发者

Strange jQuery error in IE

IE gives me an error here, where FireFox works just fine:

  $("#searched").text("");
  searched = $("messages > searched", xml).text();

The first 开发者_Go百科line empties my < div >. The second line grabs a value out of my xml result. The error is specifically on the second line. It says the object doesn't support this property or method.

Thanks!


I have seen strange errors in IE when the Id of the element is the same as the variable, like this:

var my_id = document.getElementById('my_id');

Weird? Yes indeed, but I've seen it happening more than once.

Solution: rename your variable


searched = $(xml).find("messages > searched").text();


Is messages your root node? i.e. your xml structure is as follow:

<?xml version="1.0" encoding="utf-8"?>
<messages>
  <searched>blah</searched>
  <searched>boo</searched>
</messages>

I am assuming your xml is a jquery object, i.e.

var xml = $("<?xml ...?><messages><searched>...</searched>...</messages>");
var searched = $("messages > searched", xml).text(); //this should assign blahbloo to searched.

In my experiments, when messages is the root node, you won't be able to do this, but if you change your xml input and wrap messages inside another root element, it works just fine on FF, Chrome and IE.

Like so:

<!DOCTYPE html>
<html>
  <head>
    <title>Test</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
  </head>
  <body>
    <div class="container">
    </div>
  </body>

  <script type="text/javascript">
    $(function() {
      var input = "<?xml version=\"1.0\" encoding=\"utf-8\"?><foo><messages><searched>Searched Node 1</searched><searched>Searched Node 2</searched></messages></foo>";

      var xml = $(input);

      alert($("messages > searched", xml).text());

      $("messages > searched", xml).each(function() {
        alert($(this).text());
      });
    });
  </script>
</html>

So, either wrap your xml inside another root element or just bypass the messages and just do $("searched", xml)... which should work just fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜