下面是我的代码。

var content = $("<a href=\"http://www.test.com/test?test1&test2中文\">XXXX</a>");

content.find("a").each(function() {
  var value = $(this).attr('href');
  $(this).attr('href', encodeURI(value));
  alert(value);
});​


但是,它一直显示错误。
如何使此代码起作用,以对URL进行编码。

最佳答案

代替content.find('a')使用content.filter('a')。因为现在您的content是一个只有一个元素(即<a>)的数组,所以该<a>中不再有<a>,并且.find('a')在这里失败。

因此.filter()是安全使用的。

Demo

09-25 16:53
查看更多