jQuery replaceWith()可以这样做吗?

时间:2012-09-26 06:23:23

标签: jquery

是否可以使用jQuery replaceWith来更改从TMBOBJ开始的id的div卡车

<div id="TMBOBJ7DC931A601118D2" style=" position:absolute; top:40px; left:446px; width:150px; height:100px;  z-index:2;">
<a href="Untitled_1/imag000A.html" onclick="newWin('Untitled_1/imag000A.html',333,500);return false;" target="_blank">
<img src="Untitled_1/imag000.jpg" width=150 height=100 border=0>
</a>
</div>


<div id="TMBOBJ7DC931A60C7D2" style=" position:absolute; top:26px; left:52px; width:150px; height:100px;  z-index:3;">
<a href="Untitled_1/imag001A.html" onclick="newWin('Untitled_1/imag001A.html',333,500);return false;" target="_blank">
<img src="Untitled_1/imag001.jpg" width=150 height=100 border=0>
</a>
</div>

......进入这些?

<div id="TMBOBJ7DC931A601118D2" style=" position:absolute; top:40px; left:446px;    width:150px; height:100px;  z-index:2;">
<a class="new" href="Untitled_1/imag000A.jpg">
<img src="Untitled_1/imag000.jpg" width=150 height=100 border=0>
</a>
</div>

<div id="TMBOBJ7DC931A60C7D2" style=" position:absolute; top:26px; left:52px; width:150px; height:100px;  z-index:3;">
<a class="new" href="Untitled_1/imag001A.jpg">
<img src="Untitled_1/imag001.jpg" width=150 height=100 border=0>
</a>
</div>

我甚至无法通过将.html改为.jpg只进行一次。感谢。

2 个答案:

答案 0 :(得分:0)

你可以tr y这个选择器来获得以

开头的div
$('[id^=TMBOBJ]')  // Get all elements with id 's that start with TMBOBJ

$('[id^=TMBOBJ]').each(function() {

     var $a = $(this).find('a');
     $a.attr('href' , $a.attr('href').replace('html', 'jpg'));
     $a.removeAttr('onclick');
     $a.removeAttr('target');

});

检查FIDDLE

答案 1 :(得分:0)

您尝试使用此代码:

$('div[id^=TMBOBJ]').each( function (index, element) {
    var href = $(element).children('a').attr('href');
    $(element).children('a').attr('href', href.replace('.html', '.jpg')).addClass('new');
});