2016-11-16 52 views
1

我試圖給我的管理員一個mouseover的元素,他們徘徊在給他們一個線索如何編輯,如果有的話。這裏是我的代碼:獲取元素和ID,如果有鼠標懸停與jQuery

$(document).tooltip(); 
$(document).on('mouseover','*',function(){ 
    $(this).attr('title',$(this).prop('id')); 
    console.log($(this)); 
}).on('mouseout',function(){ 
    $(this).attr('title',''); 
}); 

這對於具有id的元素非常適用。我需要得到的是html元素和id(如果有)鼠標懸停的組合。

+0

你是什麼意思*「組合html元素」*? –

+0

不清楚。在鼠標懸停時,您正在設置元素的標題。但是,沒有身份證時會發生什麼? – asprin

+0

什麼,我試圖讓是元素(身體,DIV,跨度等),這將是有用的,但沒有必要有元素 – phpmydev

回答

1

如果我正確地理解了你,當你將鼠標懸停在一個元素上時,你需要元素的id以及元素類型(標籤名稱)。如果是這樣的話,那麼你可以使用以下命令:

$(document).on('mouseover','*',function(){ 
     var id = this.id; 
     var elementType = $(this).prop('nodeName'); // will give you element tag name 
     // do something with id and elementType 
    }).on('mouseout',function(){ 
     $(this).attr('title',''); 
    }); 
+0

這樣的代碼感謝你們,我現在知道其實有其他帖子提出了同樣的問題。我應該刪除我的帖子嗎? – phpmydev

+0

@phpmydev你不需要刪除帖子。如果您接受重複問題,您的問題可以作爲其他人尋找相同答案的路標。 – 4castle

0

您應該能夠使用的this.id代替$(this)... 同時檢查您的控制檯,並挑選瓦特/電子,你從那裏需要和使用||運營商短代碼挑選最適合你的人。

$(document).on('mouseover','*',function(){ 
    $(this).attr('title',(this.id || some_other_things || yet_another_one)); 
    console.log($(this)); 
    console.log(this); 
}).on('mouseout',function(){ 
    $(this).attr('title',''); 
}); 
0

如果id沒有定義,那麼得到的DOM元素的tagName財產。

$(this).attr('title', this.id || this.tagName); 
相關問題