$.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)
但基本上,没有设置变化.
$(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)
不行.
这个:
$('img', html).each(function(){
$(this).attr("src", "http://google.com/logo.jpg");
});
Run Code Online (Sandbox Code Playgroud)
手段,大致:
htmlimg该对象中的任何标记就是这样.您不存储该对象,因此您无法使用其更新的内容 - 并且不会(或应该)更改任何内容html.
尝试改为:
var d = $(html);
$('img', d).each(function() {
$(this).attr("src", "http://google.com/logo.jpg");
});
$('a', d).each(function() {
$(this).attr("href", "http://google.com/");
});
$('body').html(d.html());
Run Code Online (Sandbox Code Playgroud)
虽然each()如果所有属性都相同,您可以跳过调用:
var d = $(html);
$('img', d).attr("src", "http://google.com/logo.jpg");
$('a', d).attr("href", "http://google.com/");
$('body').html(d.html());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
117 次 |
| 最近记录: |