我想在父項中顯示「OPEN」或「CLOSED」(取決於條件),但我不知道如何執行此操作。此代碼使兩個<h2>
標題作爲下拉列表工作。我想要函數傳遞參數,如果它不等於1,我想在<h2>
的<span>
中顯示OPEN。JQuery - 父母
的Javascript
<script type="text/javascript">
var a = null;
var b = null;
var c = 1;
$(document).ready(function() {
$(".toggle_container").hide();
$("h2.trigger").click(function() {
$(this).toggleClass("active").next().slideToggle("slow");
});
});
function divide(a, b) {
(a/b != c) ?
// Where help is needed
// $(this).parent(".status").text("OPEN").css("color", "green") :
// $("team").parent(".status").text("CLOSED").css("color","red");
document.write(a + "/" + b);
}
</script>
HTML
<body>
<div class="tournaments">
<h2 class="trigger">Tournament 1<span class="status"></span></h2>
<div class="toggle_container">
<div class="block">
<h3>Details</h3>
<p>Deadline: DATE</p>
<p>Teams: <span class="team"><script type="text/javascript">divide(3, 7)</script></span>
</p>
</div>
</div>
<h2 class="trigger">Tournament 2 <span class="status"></span></h2>
<div class="toggle_container">
<div class="block">
<h3>Details</h3>
<p>Deadline: DATE</p>
<p>Teams: <span class="team"><script type="text/javascript">divide(3,7)</script></span>
</p>
</div>
</div>
</div>
</body>
因此,當您使用'.closest'時,它會從您當前在DOM樹中的位置找到最近的「toggle_」容器。一旦你到達'.prev()',我迷路了。之前的元素是否在當前元素之前?爲什麼你不能使用'.closest('。trigger')'? –