2014-01-13 25 views
1

我已經編寫了一堆代碼(JS fiddle project),而且我快完成了,但是我需要某種切換的東西,當我點擊一個人的臉部時,然後引號(向下滾動結果如果你看不到它)將改變爲該人的報價。當選擇臉部時更改塊引號

我已經得到了在JS的開關與此部分:

$(function() { 
    var $profiles = $("#profile1, #profile2, #profile3, #profile4, #profile5, #profile6, #profile7, #profile8, #profile9, #profile10, #profile11"); 
    $profiles.click(function(e) { 
     $profiles.removeClass("focused"); 
     $(this).addClass("focused"); 
    }); 
}); 

但我需要把它與某種方式引號結合起來。

如果你真的不想幫我解決具體的問題,我只會對僞代碼感到滿意,或者幫助你思考。我一直在坐着這件作品,現在已經過了很長時間,現在可以直接思考了。

你會推薦什麼?

乾杯,你們搖滾!

回答

1

我解決你的問題是這樣的:

$profiles.click(function(e) { 
    var profileNr = parseInt($(this).attr("id").substring(7)); 
    $profiles.removeClass("focused"); 
    $(this).addClass("focused"); 
    $(".thequote .show").removeClass("show"); 
    $(".thequote blockquote").eq(profileNr-1).find("p").addClass("show"); 
}); 

它提取從輪廓數報價的指數,然後選擇使用jQuery EQ適當報價()。我更新了你的JS小提琴,並測試它的工作原理。

+0

夥計,那完全是殺手!謝謝一堆! –

1

你必須在你的blockquote中有某種引用,例如一個數據屬性,用於指示引用屬於哪個配置文件。例如:

<blockquote data-profileid="profile8"> 
    <p> 
     "Sist är starkast... eller något sånt." 
    </p> 
</blockquote> 

然後在onclick函數中,顯示帶有與所點擊的id相匹配的data屬性的blockquote。你可以得到的點擊ID是這樣的:

var id = $(this).attr('id'); 
相關問題