Prevent text selection when dragging from outside target in IE7/8

回眸只為那壹抹淺笑 提交于 2019-12-24 03:35:12

问题


It has been discussed how to disable text from being selected: How to disable text selection highlighting using CSS?

However, I have yet to find a solution that blocks the user from selecting text when dragging from outside the intended target. I'm looking for a solution that works in IE 7/8.

Any ideas?


回答1:


The IE8- onselectstart event and unselectable attribute solutions have already been discussed.

The CSS solution has been published as well. Here is the gist:

<!-- save this file as unselectable.htc and remember where you put it -->
<public:component lightweight="true">
    <public:attach event="ondocumentready" onevent="unselectable()" />
    <script type="text/javascript">
        function unselectable(){
            element.onselectstart = function(){ return false; };
            element.setAttribute('unselectable', 'on', 0);
        }
    </script>
</public:component>

/* add this rule to the existing CSS file */
.unselectable {
    user-select: none;
    -moz-user-select: none;
    -khtml-user-select: none;
    behavior: url(unselectable.htc); /* change this path as needed */
}


来源:https://stackoverflow.com/questions/9692036/prevent-text-selection-when-dragging-from-outside-target-in-ie7-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!