最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable click on a div - Stack Overflow

programmeradmin0浏览0评论

I want to disable click on a certain <div> so that when you select the text on it, the text doesn't select.

I tried writing onclick="return false;" like in this fiddle / but it doesn't work.

How can it work? (My goal isn't just disabling the select, it's the whole click).

I want to disable click on a certain <div> so that when you select the text on it, the text doesn't select.

I tried writing onclick="return false;" like in this fiddle http://jsfiddle.net/mageek/X6hqD/ but it doesn't work.

How can it work? (My goal isn't just disabling the select, it's the whole click).

Share Improve this question edited Jun 25, 2012 at 9:35 Alexandre Khoury asked Jun 25, 2012 at 9:28 Alexandre KhouryAlexandre Khoury 4,0225 gold badges38 silver badges59 bronze badges 3
  • 2 try a search on stackoverflow : stackoverflow.com/questions/920049/… – benoît Commented Jun 25, 2012 at 9:33
  • 1 @benoît Thank you, can you post this as an answer so I can accept it? – Alexandre Khoury Commented Jun 25, 2012 at 9:37
  • possible duplicate of css rule to disable text selection highlighting – Sarfraz Commented Jun 25, 2012 at 14:58
Add a comment  | 

4 Answers 4

Reset to default 8
document.getElementById('div1').onmousedown = new function ("return false");

You can try this CSS

<style type="text/css" media="print,screen" >
  .unselectable {
     -webkit-touch-callout: none;
     -webkit-user-select: none;
     -khtml-user-select: none;
     -moz-user-select: none;
     -ms-user-select: none;
     user-select: none;
   }
    </style>

<div class="unselectable">
asdasdsadsadsad<br>asdasdasd
</div>

<br><br>

<div>
qqqqqqqqqqqqqqqq<br>asdasdasd
</div>

If you want to disable selection of the text only, use:

<div onselectstart='return false;'>
    text
</div>

div{
   -moz-user-select: none;-webkit-user-select: none;
   -khtml-user-select:none;o-user-select:none;
   user-select: none;
}
<div id="div-test-id" style="width:100px;height:100px;background-color:yellow;">Lorem ipsum</div>

<script type="text/javascript">
var div_click_escape=function(eventObject){
      return false;
};

$(function(){
    $("#div-test-id").click(div_click_escape);
    $("#div-test-id").mousedown(div_click_escape);
    $("#div-test-id").mouseup(div_click_escape);
}
</script>

Will avoid to select the text.

发布评论

评论列表(0)

  1. 暂无评论