zoukankan      html  css  js  c++  java
  • JS魔法堂:阻止元素被选中

    一、前言                            

      在为IE5.5~9polyfill HTML5新特性placeholder时需要阻止元素被选中,因此在网上、书上查阅相关资料,记录在此以便日后查阅。

    二、IE10+实现方式──CSS3                    

    .unselect {
        -webkit-user-select: none; 
        -moz-user-select: none;    
        -khtml-user-select: none;  
        -ms-user-select: none;    
    
        /* 以下两个属性目前并未支持,写在这里为了减少风险 */
        -o-user-select: none;
        user-select: none;  
    }

    user-select: auto; => 用户可以选中元素中的内容

    user-select: none; => 用户不可选中元素中的内容

    user-select: text; => 用户可以选中元素中的文字

    目前这个 user-select 兼容 Chrome 6+、Firefox、IE 10+、Opera 15+、Safari 3.1+。

    三、IE5.5~9的实现──unselectable属性                

    <span unselectable="on"></span>

    由于unselectable属性不具有继承性,所以要遍历所有子元素并为各子元素添加该属性才有效。

    // 将元素及其后代元素均设置为不可选择
    var unselectable = function(root){
      root.setAttribute('unselectable', 'on');
      var descendant = root.getElementsByTagName("*");
      var rTagName = /input|iframe|textarea|select/i;
      for (var i = 0, el; el = descendant[i++];){
        if (!rTagName.test(el.tagName)){
          el.setAttribute('unselectable', 'on');
        }  
      }
    };

    四、参考                             

      《JavaScript框架设计》──9.3.2 user-select

       http://www.html-js.com/article/The-Laispace-block-element-is-selected-and-clear-the-check-method

  • 相关阅读:
    java List转换为字符串并加入分隔符的一些方法总结
    jackson 实体转json 为NULL或者为空不参加序列化
    马云告别演讲
    Linux chmod命令
    Linux执行shell脚本的方法
    2019第36周日
    2019第36周六
    eclipse中的maven插件
    SpringBoot要点之使用Actuator监控
    eclipse隐藏的列编辑
  • 原文地址:https://www.cnblogs.com/fsjohnhuang/p/3938597.html
Copyright © 2011-2022 走看看