0

是否可以使用 jQuery replaceWith 来更改一卡车 id 以 TMBOBJ 开头的 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 一个。谢谢。

4

2 回答 2

0

您可以尝试使用此选择器来获取以

$('[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');

});

检查小提琴

于 2012-09-26T06:26:37.600 回答
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');
});
于 2012-09-26T06:28:26.557 回答