jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 禁用永久活动状态大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个链接,如果拖动该链接然后释放,该链接将保持其活动状态.

例:http://jsfiddle.net/Ek43k/3/

<a href="javascript:void(0);" id="foo" >Drag me</a>
#foo:active{
    color:red;
}

我该如何防止这种情况?

(仅在IE和FF中)

解决方法

从DOM树中分离并重新连接链接,以禁用其活动状态.拖曳结束时,您有 got this
$('a').on('dragend',function(){
    var $this   = $(this),$parent = $this.parent(),$next   = $(this.nextSibling); // $.next() doesn't include text
    $this.detach().insertBefore($next);     
});

不需要混淆你的HTML或CSS,或者取消:active.似乎在FF和IE工作.

编辑:我通常不会为DOM处理写入纯Java,所以质量可能不是一流的,但是here it is without jQuery

(function(){
    var prevIoUsOnload = window.onload || function noop(){};

    window.onload = function (){

        // Call any prevIoUsly added onload callBACks
        prevIoUsOnload();

        // Add deactivator to each <a> element
        var elements = document.getElementsByTagName('a');
        for (var i=0; i<elements.length; i++){
            elements[i].ondragend = deactivate;
        }

        function deactivate(){
            var parent   = this.parentNode,position = this.nextSibling;
            parent.removeChild(this);
            // Using insertBefore instead of appendChild so that it is put at the right position among the siblings
            parent.insertBefore(this,position);
        }
    };

})();

虑到一些问题,让它完全即插即用.在Opera,Chrome,Firefox和Internet Explorer中进行测试.

编辑2:由Chris启发,另一种应用该修复的方法是直接使用ondragend属性连接去激活器(未测试):

<head>
    <script>
        function deactivate(){
            var parent   = this.parentNode,position);
        }
    </script>
</head>
<body>
    <a href="#" ondragend="deactivate()">Drag me</a>
<body>

缺点是它需要在每个链接上手动/明确地指定具有javascript的ondragend属性.我想这是一个偏好的问题.

Final(?)编辑:查看这些和Chris的回答的代表/实时版本的评论.

大佬总结

以上是大佬教程为你收集整理的jquery – 禁用永久活动状态全部内容,希望文章能够帮你解决jquery – 禁用永久活动状态所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。