2012-02-28 45 views
1

我是jQuery的新手,探索一下它的語法。如何選擇以字符串開頭但以jQuery中不同字符串結尾的內容?

我的頁面有這樣的內容:

<area id="SA" ... /> 
<area id="AF" ... /> 
<area id="EU" ... /> 
<area id="NA" ... /> 

我想根據點擊事件關閉area標籤,該標籤具有匹配的結束ID的,編碼這樣的顯示和隱藏div部分:

<div id="div_SA" ... /> 
<div id="div_AF" ... /> 
<div id="div_EU" ... /> 
<div id="div_NA" ... /> 

因此,要顯示完全匹配,但要隱藏所有以「div_」開頭但不匹配的所有div部分,而不是在頁面上隱藏其他所有div,我嘗試了第是:

var q = 'div[id="div_' + event.target.id + '"]'; 
    var r = 'div[id^="div_"],div:not[id$=' + event.target.id + '"]'; 
    $(q).show(); 
    $(r).hide(); 

$(r).hide();不起作用。我究竟做錯了什麼? (我知道我可以分配CSS類並使用類名獲得它們,但是我仍然很好奇如何構建可以這種方式工作的查詢。)

+1

對於評估q,我會簡單地使用q ='#div_'+ this。ID; – 2012-02-28 20:16:44

+0

那麼顯然有不止一種方式來做到這一點。我查了一下我用過的那個,它正在工作,謝謝你的演示。 – 2012-02-28 22:41:27

回答

1

編輯:

var r = 'div[id^="div_"],div:not(#' + event.target.id + ')'; 

而且,你的代碼可以通過將其更改爲簡化在您的文章中提到的語法修正見下文,

var q = '#div_' + this.id; 
    var r = 'div[id^="div_"]:not("#div_' + this.id + '")'; 

    $(r).hide(); 
    $(q).show(); 

DEMO

請檢查下面的替代解決方案,

爲了評估Q,我會簡單地使用

var q = $('#div_' + this.id); 

而對於R,

var r = $('div[id^="div_"]').not(q); 

r.hide(); 
q.show(); 

DEMO

1

not css僞選擇器不使用括號括起括號。在最後一個括號之前,您還有一個無與倫比的引號。

var q = '#' + event.target.id; 
var r = 'div[id^="div_"]:not(#' + event.target.id + ')'; 

$(q).show(); 
$(r).hide(); 
0
//start by selecing all of the DIV elements that have an ID attribute that starts with "div_", 
//and hide all of them, 
//then filter that list down to only the element(s) that have an ID that ends with the id of the `event.target` element 
//and show that/those element(s) 
$('div[id^="div_"]').hide().filter('#div_' + event.target.id).show(); 
+0

這將隱藏所有div,然後顯示選定的一個..這將給你一個閃爍效果。 – 2012-02-28 20:21:22

+0

''[#div_''+ event.target.id +'「]''選擇器是錯誤的。 – ShankarSangoli 2012-02-28 20:22:56

+0

@SKS - 它在微秒內不會產生任何閃爍效果。 – ShankarSangoli 2012-02-28 20:23:49

1

如何:

$('area').click(function() { 
    var areaID = $(this).attr('id'); 
    $('div[id^="div_"]').hide(); 
    $('div[id^="div_' + areaID + '"]').show(); 
});​ 
+0

隱藏整個頁面,由'div's – 2012-02-28 22:00:49

+0

@RobPerkins - 良好的捕捉,代碼更新。 – j08691 2012-02-28 22:04:55

0

簡短

// cache divs, no need to search them again 
var divs=$('div[id^="div_"]'); 
$('area').click(function(){ 
     divs.hide().eq($(this).index()).show();     
}); 
2

讓事情容易和簡單越好,因爲你是在新的jQuery,你應該使用on()的習慣。不是click(),那已經過時了,只是簡單地指的是on方法。

$('area').on('click', function() { 
    var id = "#div_" + $(this).attr('id'), // comma allows you to make multiple variables 
     divs = $('div').hide() // puts all the divs in a variable and automatically hides them all 

    // filter through all the divs, and selects the one with the id, 
    // of the area that was clicked, and shows it 
    divs.filter(id).show(); 
});​ 

希望這可以幫助你現在。如果沒有,請讓我知道。

+0

對不起,你能解釋爲什麼click()已經過時了嗎?或者使用on()的優點?請。 PD:遺憾的是,只要我記得總是過時的話,'.click()'只有我的記憶 – 2012-02-28 22:15:24

+1

。它在那裏讓事情變得更簡單。 '.click()','.bind()','.live()'和'.deligate()'做的是他們都參考'.on()'方法('.unbind()'指的是'.off()'。)因此,不要總是引用'.on()'方法,最好使用'.on()'方法。 ('click',fn)'('div')。click(fn)'='$('div')。on('click',fn) 示例: _you將.on()綁定到要單擊的元素_ ' – 2012-02-29 05:13:23

+0

如果你正在動態添加到dom,將.on()綁定到已經在dom,父元素中的東西,並監聽它:_ $('div')。live('click', fn)'='$(document).on('click','div',fn)'。 – 2012-02-29 05:21:19

相關問題