2012-02-21 50 views
0

我們在頁面p標籤,我們將檢測與JavaScript ONSELECT或這樣的事情,也許這選擇可以通過鼠標或鍵盤來觸發選擇這一點,我們也不能使用onmouseup或onclick!的JavaScript ONSELECT爲p標籤

<p onselect="alert('selected')" >My p text is here ...</p> 

onselect with dropdown and textbox,and not work with p tag。

+0

正如你所說的,'select'不是'p'標記引發的事件。您可以改爲使用'onclick',但您在此使用的方法遠不是最佳實踐。你想達到什麼目的? – 2012-02-21 10:17:04

+0

也許用鍵盤選擇!那麼我的方法不起作用! – 2012-02-21 10:20:16

回答

-2

缺省情況下p TATG是不可選的,你可以將它設置可選擇與此短代碼:

$('p') 
    .attr("tabIndex", 0) 
    .css({"-moz-user-select": "none","-webkit-user-select": "none","-khtml-user-select": "none"}) 
    .attr("unselectable", "on"); 
+1

我認爲你的回答有點落後。 – 2012-02-21 10:21:23

+0

您描述的代碼的功能和實際的功能有兩點不同。 – 2012-02-21 10:31:01

+0

... noitseuq eht dootsrednusim evah tsum I,yrroS – sinsedrix 2012-02-21 10:37:27

0

你可以使用的onclick,如果它適合你的需要,那麼:

 

$("body").delegate("p", "click", function(){ 
    alert('selected'); 
}); 
 

希望它有幫助

+0

也許通過鍵盤選擇 – 2012-02-21 10:22:12

+0

@Majid Shahmohammadi。這將調用jquery $(document).keydown(function(e){alert(e.keyCode)})的按鍵。你會得到哪個按鍵 – 2012-02-21 10:26:50

+0

我知道這是按鍵和鼠標或點擊,但我會通過選擇工作,因爲必須使用兩種方法來檢測到這一點。 – 2012-02-21 10:31:34

0

在Internet Explorer和Webkit中,您可以使用:

somedom.addEventListener('selectstart',callback) 
+0

請記住,此事件在您開始選擇時觸發,而不是在完成選擇時觸發。 – 2012-02-21 10:52:37

+0

var obj = document.getElementById('test2'); obj.addEventlistener('selectstart',function(){ \t alert(234); }) – 2012-02-21 11:09:12

+0

obj.addEventlistener不是一個函數! – 2012-02-21 11:09:36