js禁止选中图片和文字


在写一个图片查看的插件时,需要禁止选中图片和文字,方法如下:

 if(document.all){
        document.onselectstart= function(){return false;}; //for ie
   }else{
        document.onmousedown= function(){return false;};
        document.onmouseup= function(){return true;};
  }
  document.onselectstart = new Function('event.returnValue=false;');

但是这种方法在执行完后 会影影响页面的其他元素,比如input不能获取焦点,更好的写法是:

ie:document.selection.empty() 
ff:window.getSelection().removeAllRanges() 

兼容性的写法(不仅不影响选中效果,而且能清楚对其他元素影响):

window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM