我想使用jQuery打开和关闭div。 但是因为我使用的代码是动态的,并且会在彼此之下重复,所以我需要使用动态ID。jquery使用动态编号打开div
HTML:
<div class="categoryratings-review-<?php echo $this->htmlEscape($_review->getId())?>" style="display: none;">
<p>Text</p>
</div>
<span class="showcategoryratings" id="review-<?php echo $this->htmlEscape($_review->getId())?>">
<span class="showcategoryratings-text">Per category</span>
</span>
我尝试使用这个jQuery的,但我猜的PHP线不工作:
$(document).ready(function() {
$('#review-<?php echo $this->htmlEscape($_review->getId())?>').click(function() {
$('.categoryratings-review-<?php echo $this->htmlEscape($_review->getId())?>').slideToggle("fast");
$(this).toggleClass('active');
});
});
如何需要正确地编辑呢?
你需要把你的JS代码在PHP渲染,而不是在外部文件中的HTML。 – lmgonzalves