4 回答

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
我相信這對你有用。
for (let i = 1; i <= 15; i++) {
$(".main" + i).click(function(){
$(".show" + i).slideToggle("slow");
});
}

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
在每個(gè)元素上放置相同的類,然后使用data屬性來定位您想要的不同實(shí)例slideToggle()。使用此方法,相同的三行 JS 將適用于 HTML 中無限數(shù)量的相關(guān)元素。
$(".main").click(function() {
$(this.dataset.target).slideToggle("slow");
});
.show { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="main" data-target="#show-1">Show 1</a>
<a href="#" class="main" data-target="#show-2">Show 2</a>
<a href="#" class="main" data-target="#show-3">Show 3</a>
<div class="show" id="show-1">Lorem ipsum</div>
<div class="show" id="show-2">Dolor sit</div>
<div class="show" id="show-3">Amet consectetur</div>

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
遍歷DOM解決方案
使用this
關(guān)鍵字并遍歷 DOM
這意味著“通過”,用于根據(jù)與其他元素的關(guān)系“查找”(或選擇)HTML 元素
就是這樣。
例如:如果內(nèi)容是按鈕的下一個(gè)兄弟元素,請使用next()(2 行代碼)。
$('button.slider_trigger').click(function() {
? $(this).next().slideToggle("slow");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
? <button class="slider_trigger">Toggle 1</button>
? <p class="content">
? ? This is the paragraph 1 to end all paragraphs.? You
? ? should feel <em>lucky</em> to have seen such a paragraph in
? ? your life.? Congratulations!
? </p>
</div>
<div>
? <button class="slider_trigger">Toggle 2</button>
? <p class="content">
? ? This is the paragraph 1 to end all paragraphs.? You
? ? should feel <em>lucky</em> to have seen such a paragraph in
? ? your life.? Congratulations!
? </p>
</div>
如此處其他答案所述,最好的方法是使用mainand showclasses 而不是main1,main2等main3。
如果由于某種原因你不能改進(jìn)你的 html 結(jié)構(gòu),你可以使用 jquery 來regex定位元素:
$("[class^=main]").click(function(Event) {
? ? var id = this.className.match(/main(\d+)/)[1];
? ? $(".show" + id).slideToggle("slow");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="main1">Toggle 1</button>
<p class="show1">
? This is the paragraph 1 to end all paragraphs.? You
? should feel <em>lucky</em> to have seen such a paragraph in
? your life.? Congratulations!
</p>
<button class="main2">Toggle 2</button>
<p class="show2">
? This is the paragraph 2 to end all paragraphs.? You
? should feel <em>lucky</em> to have seen such a paragraph in
? your life.? Congratulations!
</p>
如此處其他答案所述,最好的方法是使用mainand showclasses 而不是main1,main2等main3。
如果由于某種原因你不能改進(jìn)你的 html 結(jié)構(gòu),你可以使用 jquery 來regex定位元素:
$("[class^=main]").click(function(Event) {
? ? var id = this.className.match(/main(\d+)/)[1];
? ? $(".show" + id).slideToggle("slow");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="main1">Toggle 1</button>
<p class="show1">
? This is the paragraph 1 to end all paragraphs.? You
? should feel <em>lucky</em> to have seen such a paragraph in
? your life.? Congratulations!
</p>
<button class="main2">Toggle 2</button>
<p class="show2">
? This is the paragraph 2 to end all paragraphs.? You
? should feel <em>lucky</em> to have seen such a paragraph in
? your life.? Congratulations!
</p>

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
也許嘗試使用 for 循環(huán):
for (let i=1; i<16; i++){
$((".main"+i.toString())).click(function(){
$((".show"+i.toString())).slideToggle("slow");
});
}
添加回答
舉報(bào)