让我坐下来试图搜索这段代码:
<div id="ad_creative_1" class="ad-div mastad" style="z-index: 1;">
<script>(function() {var loaded = function() {return yt && yt.www && yt.www.home && yt.www.home.ads;};window.masthead_ad_creative_iframe_1_workaround = function() {if (loaded()) {yt.www.home.ads.workaroundIE(this);}};window.masthead_ad_creative_iframe_1_onload = function() {if (!loaded()) {setTimeout(masthead_ad_creative_iframe_1_onload, 50);return;}yt.www.home.ads.workaroundLoad();};})();</script>
<iframe id="ad_creative_iframe_1" src="http://ad-g.doubleclick.net/N4061/adi/com.ythome/_default;sz=970x250;tile=1;plat=pc;dc_dedup=1;kage=18;kar=3;kbsg=HPUS130404;kcr=us;kga=1001;kgender=m;kgg=1;klg=en;kmyd=ad_creative_1;"
height="250" width="970"
scrolling="no" frameborder="0" style="z-index: 1"
onload="masthead_ad_creative_iframe_1_onload();"
onmouseover="masthead_ad_creative_iframe_1_workaround(this)"
onfocus="masthead_ad_creative_iframe_1_workaround(this)"></iframe>
<script>
(function() {
var ord = Math.floor(Math.random() * 10000000000000000);
var adIframe = document.getElementById("ad_creative_iframe_1");
adIframe.src = "http://ad-g.doubleclick.net/N4061/adi/com.ythome/_default;sz=970x250;tile=1;plat=pc;dc_dedup=1;kage=18;kar=3;kbsg=HPUS130404;kcr=us;kga=1001;kgender=m;kgg=1;klg=en;kmyd=ad_creative_1;ord=" + ord + "?";
})();
</script>
</div>
我想在此代码中搜索“doubleclick.net”,并在不知道父div ID的情况下找到父div“ad_creative_1”。
我试过这个:
var container = document.getElementById.$('[src*="doubleclick.net"]').parent().getAttribute('id');
container.innerHTML = '<center><iframe src="http://somesite.com/" width="478" height="70" scrolling="no" /></center>';
container.id = "randomshithere";
我想用container.innerHTML
替换整个div如果我取出选择器,并为getElementId添加ad_creative_1,它可以正常工作并完成我需要的工作,但是选择器在实际获取div时会给我一些问题ad_creative_1
非常感谢任何帮助。
答案 0 :(得分:2)
您可以使用.closest()
获取div:
var parentDivElement = $('[src*="doubleclick.net"]').closest("div");
parentDivElement.html("<b>Whatever you'd like </b>");