2016-07-07 33 views
0

這裏我帶了一個jQuery問題。考慮這個隨機元素:jQuery:具有相同標記名稱的兄弟姐妹的數量

<div id="some_random_id"> 
    <b></b> 
    <div></div> 
    <b></b> 
    <div></div> 
    <div></div> 
</div> 

我們所擁有的是一個指向子元素。我們對父元素一無所知。我們需要編寫一個函數,輸出具有相同標記名的兄弟的NUMBER個數。

function n_siblings_same_tagname(this){}; 

函數必須返回2時,我們的目標是DIV標籤名和1時,標籤名稱是B.我們如何做到這一點?由於

+0

使用id =的DIV中的其他申報單 'some_random_id' 都是孩子,不是兄弟姐妹。你能否改變標題以反映這一點? – DinoMyte

+0

是的,沒錯。我們對父元素一無所知。我們知道的只是一個指向子元素的指針,而其他子元素是元素的兄弟元素,我們知道 – user3600124

+0

如果我們返回這些數字,那麼您不會檢索具有給定標籤名稱的兄弟的數量,您會發現所有帶有標籤名稱的孩子,兄弟姐妹加上原創。請你可以[編輯]你的問題,以確切地指定你想要的? –

回答

3

我建議:

function n_siblings_same_tagname(node){ 

    // Retrieve the tagName of the passed-in 
    // DOM node: 
    var elType = node.tagName; 

    // Converting the children the passed-node's 
    // parent's element children into an Array, 
    // using Array.from(): 
    return Array.from(node.parentNode.children) 

    // filtering that array, using an Arrow 
    // which retains only those elements 
    // whose tagName is equal to the 
    // tagName of the passed-in elementary: 
     .filter(elem => elem.tagName == elType) 
    // finding the length of the filtered Array, and 
    // subtracting 1, as we want siblings not all 
    // elements of that type: 
     .length - 1; 
} 

或者,如果jQuery是首選:

function n_siblings_same_tagname(element){ 

    // we may receive a jQuery object, or a DOM 
    // node; here we wrap the received element 
    // with jQuery: 
    var el = $(element); 

    // returning the length of the number of 
    // sibling elements matching the 
// supplied selector, here the tagName 
// of the passed in element, and 
    return el.siblings(el.prop('tagName')).length 
} 

鑑於OP的迴應,在下面的評論中,我會提供以下替代方案:

function n_siblings_same_tagname(element){ 

    // we may receive a jQuery object, or a DOM 
    // node; here we wrap the received element 
    // with jQuery: 
    var el = $(element); 

    // returning the length of the number of 
    // child elements matching the selector 
    // provided by the tagName property 
    // of the passed-in element: 
    return el.parent().children(el.prop('tagName')).length 
} 

或者,修改後的JavaScript:

function n_siblings_same_tagname(node){ 

    // Retrieve the tagName of the passed-in 
    // DOM node: 
    var elType = node.tagName; 

    // Converting the children the passed-node's 
    // parent's element children into an Array, 
    // using Array.from(): 
    return Array.from(node.parentNode.children) 

    // filtering that array, using an Arrow 
    // which retains only those elements 
    // whose tagName is equal to the 
    // tagName of the passed-in elementary: 
     .filter(elem => elem.tagName == elType) 
    // finding the length of the filtered Array: 
     .length; 
} 
+0

我的錯誤,我的意思是元素本身及其所有的兄弟姐妹。無論如何,答案的邏輯並沒有太大的改變。謝謝你的回答 – user3600124

3

這應該選擇你的父母的孩子們都用相同的標籤:

function n_siblings_same_tagname(element){ 
    element = $(element); 
    return element.parent().children(element.prop("tagName")).length; 
};  
+0

這假設'this'是一個jQuery對象而不是DOMElement,但在這種情況下可以工作。你只需要返回'length'屬性。 –

+0

感謝您的評論,我會包裝它! – Abaddon666

+0

第一行是什麼? – user3600124