我想使用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