2012-04-24 101 views
1

JQuery替换html元素内容我正在寻找移动或复制HTML元素的内容。这已经被问及之前,我可以得到innerHTML()或JQuery的html()方法工作,但我试图自动化它。如果ID以前缀

如果元素的ID以'rep_'开头,则在下划线后替换该元素的内容。

所以,

<div id="rep_target"> 
Hello World. 
</div> 

将取代:

<div id="target"> 
Hrm it doesn't seem to work.. 
</div>​ 

我已经试过:

$(document).ready(function() { 
    $('[id^="rep_"]').html(function() { 
     $(this).replaceAll($(this).replace('rep_', '')); 
    }); 
});​ 

- 和 -

$(document).ready(function() { 
    $('[id^="rep_"]').each(function() { 
     $(this).replace('rep_', '').html($(this)); 
    }); 
​});​ 

似乎都没有工作,但是,这并不工作,只能手动:

var target = document.getElementById('rep_target').innerHTML; 
document.getElementById('target').innerHTML = target; 

相关,但是这仅仅是文字。 JQuery replace all text for element containing string in id

+0

哪里更换从何而来?你想转移 - 替换(转移现有的内容来替换目标)?或克隆替换(复制现有内容并替换目标)? – Joseph 2012-04-24 00:10:06

+0

一般评论:你在你的例子中使用html错误:html将一个字符串,而不是一个函数,作为参数。同样,你正在使用替换不正确;如果你想改变属性(任何属性,包括“id”),你应该使用attr。我认为后者的困惑来自误解$(foo)返回的内容:它不是元素的ID,也不是元素本身(或元素)。相反,它是一个围绕元素的包装器,类似于[element1,element2,...],但带有数组通常不具有的额外方法。希望有所帮助。 – machineghost 2012-04-24 00:22:04

+0

我是JS noob。我需要从id =“rep_target1”中获取格式化的HTML内容,并替换或附加id =“target1”的内容。基本上,搜索所有具有id =“rep_X”的元素,然后查找并替换id =“X”,页面上有多个实例具有不同的X值...... – 2012-04-24 22:43:26

回答

2

第一部分有两个基本选项:用HTML字符串替换或用实际元素替换。

选项#1:HTML

$('#target').html($('#rep_target').html()); 

选项#2:要素

$('#target').empty().append($('#rep_target').children()); 

如果你有没有偏好,后一种选择是更好的,因为浏览器将不必重新构建所有的DOM位(只要浏览器将HTML转换为元素,它就会工作并因此影响性能;选项#2通过不让浏览器创建任何新元素来避免这种工作)。

这应该包括更换内部。你也想改变元素的ID,并且只有一个方法(我知道)

var $this = $(this) 
$this.attr($this.attr('id').replace('rep_', '')); 

所以,把他们放在一起,是这样的:

$('[id^="rep_"]').each(function() { 
    var $this = $(this) 
    // Get the ID without the "rep_" part 
    var nonRepId = $this.attr('id').replace('rep_', ''); 
    // Clear the nonRep element, then add all of the rep element's children to it 
    $('#' + nonRepId).empty().append($this.children()); 

    // Alternatively you could also do: 
    // $('#' + nonRepId).html($this.html()); 

    // Change the ID 
    $this.attr(nonRepId); 

    // If you're done with with the repId element, you may want to delete it: 
    // $this.remove(); 
}); 

应该做的伎俩。希望有所帮助。

+0

作为一个附注,您可能注意到这里有一个模式, html和attr方法:如果你在没有参数的情况下调用它们,你会返回值,如果你提供了一个参数,你可以将该参数设置为值。这个模式也适用于jQuery中的很多其他方法(text,val等)。 – machineghost 2012-04-24 00:13:39

+0

这非常接近,但我需要动态地执行选项#1。像这样:http://jsfiddle.net/SZUZt/ – 2012-04-24 22:36:03

+0

我编辑了答案,使其更清晰,你可以如何组合这两部分。我在组合版本中使用了选项#2,因为它的性能会更好,但如果您愿意,您可以轻松替换选项#1。 – machineghost 2012-04-24 23:50:10

1

使用attr方法获取的ID,去掉前缀,从它创建一个选择,您可以通过元素的HTML代码,并从函数返回它:

$('[id^="rep_"]').html(function() { 
    var id = $(this).attr('id'); 
    id = id.replace('rep_', ''); 
    var selector = '#' + id; 
    return $(selector).html(); 
}); 

或者干脆:

$('[id^="rep_"]').html(function() { 
    return $('#' + $(this).attr('id').replace('rep_', '')).html(); 
}); 
+0

+1,你也可以使用'this.id' ;-) – 2012-04-24 00:22:05

0

从我的问题,我的理解是,你想通过删除re-_前缀来替换id,然后更改该div的内容。这个脚本将做到这一点。

$(document).ready(function() { 
    var items= $('[id^="rep_"]'); 
    $.each(items,function(){ 
     var item=$(this); 
     var currentid=item.attr("id"); 
     var newId= currentid.substring(4,currentid.length); 
     item.attr("id",newId).html("This does not work");   
     alert("newid : "+newId); 
    });  

}); 

工作示例:http://jsfiddle.net/eh3RL/13/