我正在尝试删除div并将其添加回窗口调整大小的DOm上。
我在移动网页上的不同位置生成了两个google add position
。根据Google的政策,移动页只能添加一个。
但是我不得不显示谷歌根据不同的窗口大小/移动设备等在diffenret的地方添加。所以我只是使用Media Queries隐藏了其中一个。
但是由于另一个div也处于管辖区,因此违反了政策。所以我用下面的脚本
<script>
$(document).on('pagecreate','#outerPage',function(e) {
var windowWidth = $(this).width();
if(windowWidth <300)
{
$('.addBigphone').remove();
}
else
{
$('.addSmallphone').remove();
}
});
</script>
HTML看起来像这样
<div id="bloque" class="addSmallphone">
<?php // if($showadslast==t rue){?>
<div class="google_add">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- resultmobile -->
<ins class="adsbygoogle" style="display:inline-block;width:320px;height:90px" data-ad-client="ca-pub-8" data-ad-slot=""></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<div id="bloque1" class="addBigphone">
<?php // if($showadslast==t rue){?>
<div class="google_add">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- resultmobile -->
<ins class="adsbygoogle" style="display:inline-block;width:320px;height:90px" data-ad-client="ca-pub-8" data-ad-slot="04"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
现在,它仅适用于页面加载。
注意我要保存div的内容,并以不同的大小将其重新添加。我怎样才能做到这一点
提前致谢
我想这就是你想要的。在计划将元素重新插入DOM时,应该使用detach
而不是remove
。
var $bigPhoneAdd = $('.addBigphone'),
$smallPhoneAdd = $('.addSmallphone');
$(window).resize(function(e) {
var windowWidth = $(this).width();
if (windowWidth < 300) {
$bigPhoneAdd.detach();
//You should append at desired insertion point...
//Perhaps it could be made dynamic by storing $('.addBigphone').parent()
//when the page loaded.
$smallPhoneAdd.appendTo(?);
}
else {
$smallPhoneAdd.detach();
//You should append at desired insertion point...
$bigPhoneAdd.appendTo(?);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句