小编Vic*_*Vic的帖子

jQuery.attr()没有更新HTML字符串?

$.ajax({
    url: "getHTML.php",
    data: {url: "http://www.google.com"},
    success: function(e){
        var html = e;
        $('img', html).each(function(){
            $(this).attr("src", "http://google.com/logo.jpg");
        });
        $('a', html).each(function(){
            $(this).attr("href", "http://google.com/");
        });
        $('body').html(html);
    }
});
Run Code Online (Sandbox Code Playgroud)
  1. 运行AJAX
  2. 运行PHP,它在GET请求中回显URL的HTML
  3. 将响应保存为HTML字符串
  4. 使用jQuery解析HTML字符串并替换所有链接和图像
  5. 在页面中显示

但基本上,没有设置变化.

$(this).attr("src", "http://google.com/logo.jpg");
Run Code Online (Sandbox Code Playgroud)

返回带有google徽标的片段作为源属性,但是

$(this).replaceWith($(this).attr("src", "http://google.com/logo.jpg"));
Run Code Online (Sandbox Code Playgroud)

不行.

javascript jquery

0
推荐指数
1
解决办法
117
查看次数

标签 统计

javascript ×1

jquery ×1