2013-02-11 26 views
0

這是我到目前爲止。 http://jsfiddle.net/Scybf/22/jQuery添加2個ID到相等的內容

$('[id^=content]').hide(); 
$('#baseSection, #headSection').on("click", "li", function() { 
    var id = ($(this).parent().attr('id') == 'selectable') ? $(this).attr('id').replace('base', '') : $(this).attr('id').replace('head', ''); 
    $("#customContent").html($("#content-" + id).html()); 
}); 

我需要做的是採取基地和頭部的ID的平等內容。例如,base1 + head1 = content1或base1 + head2 = content2。

任何幫助將不勝感激。

+0

BASE1 + HEAD2 =內容2?而不是base2 + head2 = content2。 – JohnJohnGa 2013-02-11 13:35:10

+2

對不起,你很難理解你想要做什麼,你能告訴我們一些輸入/輸出嗎? – JohnJohnGa 2013-02-11 13:36:22

+0

這個想法是一個自定義的工具概念,所以用戶將在第1部分中單擊一個基本部分,然後在第2部分中單擊一個頭部部分,然後它將顯示組合結果,該結果將是圖像和內容。 – Jon 2013-02-11 13:42:41

回答

0

這可以幫助你:

您需要,如果用戶headbase元素上點擊定義:

function isBase(element){ 
    return element.parents('div').attr('id').indexOf('base') !== -1; 
} 

function isHead(element){ 
    return element.parents('div').attr('id').indexOf('head') !== -1; 
} 

然後您可以創建一個緩衝區對象:

var buffer = {head: null, base: null}; 

然後您只需要保存用戶操作並將其顯示在您的baseContent元素中

$('#baseSection, #headSection').on("click", "li", function() { 
    if (isHead($(this))) { 
     buffer.head = $(this).text(); 
    } else if (isBase($(this))) { 
     buffer.base = $(this).text(); 
    } 

    $("#baseContent").html(
     "Head: " + buffer.head + "<br/>Base:" + buffer.base); 
}); 
0

如果添加一個公共類,每個列表,你可以做這樣的事情:

$('.selectable').on("click", "li", function() { 
     var $li=$(this).addClass('selected') 
     $li.siblings().removeClass('selected'); 
     var contentNum= this.id.replace(/\D/g,'') 
     var $container= $li.closest('#headSection').length ? $('#headContent') : $('#baseContent'); 
     $container.html($('#content-'+contentNum).html()) 

    }); 

DEMO:http://jsfiddle.net/Scybf/20/