现在,一些replaceWith代码正在工作,另一个不是。例如,此代码的工作:如何使用replaceWith替换多个div类中的img和div ID的
$('.maincontent>h1:contains("Oakland Raiders")').replaceWith('<img src="http://i.nflcdn.com/static/site/6.3/img/subheaders/oak.png" class="subheader" />');
下面是从网站上关于如果使用在Firefox中检查元素这个特定部分的一些HTML代码:
<div class="maincontent">
<img class="subheader" src="http://i.nflcdn.com/static/site/6.3/img/subheaders/stl.png"></img>
<div id="team_wrapper">
<div id="team-overview-wrapper">
<div id="team-overview-profile">
<div id="team-overlay-container">
<img class="team-overlay" src="/img/m25/right/339.png"></img>
</div>
<div class="team-bio">
<div class="team-logo">
<div class="teamimage-left" style="background-image: url("/img/m25/left/339.png");"></div>
</div>
注意:你所不能看到的是h1元素,但您可以看到如果您查看页面源而不是检查元素。
H1的部分你不能看到在检查元素,但可以看到,如果你看一下页面的源代码,它看起来像这样:
<div class="maincontent">
<h1>Oakland Raiders</h1>
<div id="team_wrapper">
<div id="team-overview-wrapper">
<div id="team-overview-profile">
<div id="team-overlay-container">
<img class="team-overlay" src="/img/m25/right/22.png">
</div>
<div class="team-bio">
<div class="team-logo">
<div class="teamimage-left" style="background-image: url("/img/m25/left/22.png");"></div>
</div>
<div class="team-info">
我不能让工作正在取代团队覆盖图像或团队徽标图像。我尝试了以下方法,它什么都不做。
$('.maincontent>h1:contains("Oakland Raiders")').replaceWith('<img src="http://www.papermag.com/uploaded_images/cclinemustard-funny.png" class="team-overlay" />');
或
$('.maincontent>h1:contains("Oakland Raiders")').replaceWith('<img src="http://www.papermag.com/uploaded_images/cclinemustard-funny.png" class="teamimage-left" />');
我怎能取代teamimage左图像,并使用replaceWith团队,覆盖图像?我不知道为什么在顶端的代码工作,但这一个没有。
'h1'元素在哪里?正如我所想,它被替换为'img.subheader'。我错了吗? –
你可以分享你的代码,包括'h1'元素......最可能的失败原因是一个糟糕的选择器(即元素不存在)。 'replaceWith'方法应该可以正常工作:http://jsfiddle.net/vbw910qo/ – Fenton
@RadonirinaMaminiaina ive添加了h1元素。您只能在查看页面源 – user3556644