![](/img/trans.png)
[英]Javascript Show Hide - Changing toggle action from Checkbox to <a href> links
[英]JavaScript toggle 2 links from full page
我的頁面中充滿了鏈接,並且它們是配對的。 我想做的就是單擊一個問號(尋求幫助)后,先選擇前兩對,然后選擇下兩對,依此類推。 問題是鏈接是在頁面上隨機創建的。 我有以下代碼選擇第一個鏈接及其對。
$(".main .container a:first").css("color", "#0c0");
var valid = $(".main .container a:first").attr("class").split(" ");
var links = $(".main .container a");
for (i = 0; i < links.length; i ++) {
var attributes = $(links[i]).attr("class").split(" ");
if (attributes[1] == valid[1]) {
$(links[i]).eq(0).css("color", "#0c0");
}
}
編輯:
$(".help a").on("click", function()
{
var unchecked = $(".main .container a:not(.selected)");
var valid = unchecked.eq(0).attr("class").split(" ");
var links = $(".main .container a");
unchecked.eq(0).addClass("selected");
for (i = 0; i < links.length; i ++) {
var attributes = $(links[i]).attr("class").split(" ");
if (attributes[1] == valid[1]) {
$(links[i]).eq(0).addClass("selected");
}
}
});
看起來您可以通過將CSS類添加到已經“選定”的元素中來簡單地進行管理。
檢查我在JSFiddle上所做的這個簡單測試: http : //jsfiddle.net/L4rUM/1/
// When you click the "?"
$('button#btnHelp').on('click', function(){
// Gathers the list of ALL <a> elements that were not "selected" yet
var uncheckeds = $('.main .container > a:not(.selected)');
// If you have at least 2 available elements
if(uncheckeds.size() > 1){
// Adds the .selected CSS class to first 2 non-selected elements
uncheckeds.eq(0).addClass('selected');
uncheckeds.eq(1).addClass('selected');
}
});
現在,只需添加CSS類即可控制您正在執行的CSS操作:
.selected { color: #0c0; }
我希望這可以幫助您解決問題:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.