[英]Generate unique div id in php to assign javascript to it
I generate the following pieces of code via php (unknown number in advance) and they are all wrapper in my 'item-container' div: 我通过php生成了以下代码(提前提供了未知数),它们都包装在“ item-container” div中:
<div id="item-size" class="item-size">
<div class="view pic-transition">
<figure id="ribbonnew" class="ribbonnew">
<img class="ribbonnewimg" alt="" src="../images/endingsoonribbon.png">
</figure>
<img src="../images/woman.jpg" />
<div class="mask">
<h2>Title</h2>
<p>This is a test of a description for an item.</p>
<a href="#" class="info">Read More</a>
</div>
</div>
</div>
I generate a ribbon on SOME of these 'item-size' div's and via javascript i want the ribbon to be hidden when the mouse is hovered over and back to normal when mouse out. 我产生了一些这些“项目规模”的div的丝带和通过JavaScript我想要当鼠标悬停并恢复正常时,鼠标移出要隐藏的色带。 My javascript code is:
我的JavaScript代码是:
$("#item-size").hover(function(){
$('#ribbonnew').hide();
},function(){
$('#ribbonnew').show();
});
This of course only works for the first element, so I guess I need to assign ID's to the 'item-size' div's ? 当然,这仅适用于第一个元素,因此我想我需要将ID分配给“ item-size” div? How do I do this AND create the javascript which binds the mouse hover to every of these divs (how to pass the size of how many I created, so I could add ID's from 0 to size)?
如何执行此操作并创建将鼠标悬停绑定到每个div的javascript(如何传递我创建的div的大小,以便可以将ID从0添加到大小)?
As an extra question, is there also a way to make the ribbon fade in and fade out slowly? 另外一个问题是,是否还有一种方法可以使色带渐入渐出并逐渐消失? .fadeOut(1000);
.fadeOut(1000); is not delivering the expected result
没有达到预期的结果
Remove all ids : 删除所有ID:
<div class="item-size">
<div class="view pic-transition">
<figure class="ribbonnew">
<img class="ribbonnewimg" alt="" src="../images/endingsoonribbon.png">
</figure>
<img src="../images/woman.jpg" />
<div class="mask">
<h2>Title</h2>
<p>This is a test of a description for an item.</p>
<a href="#" class="info">Read More</a>
</div>
</div>
</div>
And use a dot .
并使用一个点
.
in your selectors to match elements by classes : 在选择器中按类匹配元素:
$(".item-size").hover(function(){
$(this).find('.ribbonnew').hide();
},function(){
$(this).find('.ribbonnew').show();
});
For your extra question, you can use a parameter in the hide
and show
jquery methods for animation : 对于您的其他问题,您可以在
hide
和show
动画的jquery方法中使用参数:
$(this).find('.ribbonnew').hide(400);
Edit : if the html is inserted dynamically, try event delagation instead : 编辑:如果html是动态插入的,请尝试使用事件延迟:
$('#item-container').on('mouseenter mouseleave', '.item-size', function(){
$(this).find('.ribbonnew').toggle(400);
});
(if you really want to use ids) (如果您确实要使用ID)
Generate a unique id using the uniqid()
function, and name all your item-size
elements. 使用
uniqid()
函数生成唯一的ID,并命名所有item-size
元素。
<?php
$unique_id = uniqid();
?>
<div id="<?=$unique_id?>item-size" class="item-size">
<div class="view pic-transition">
<figure class="ribbonnew">
<img class="ribbonnewimg" alt="" src="../images/endingsoonribbon.png">
</figure>
<img src="../images/woman.jpg" />
<div class="mask">
<h2>Title</h2>
<p>This is a test of a description for an item.</p>
<a href="#" class="info">Read More</a>
</div>
</div>
</div>
Then, match all elements with that unique id as part of their ids. 然后,将具有该唯一ID的所有元素作为其ID的一部分进行匹配。
$("*[id^='<?=$unique_id?>']").hover(function(){
$(this).find('figure.ribbonnew').hide();
},function(){
$(this).find('figure.ribbonnew').show();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.