• innerText兼容性问题


    /*

    text方法,给网页元素设置文本值的方法
    主要处理火狐不支持innerText这个属性的问题。
    还学习了如何判断一个字符串类型的属性是否存在
    如果判断一个对象类型的属性是否存在,用if(ele.attr)就行,但是如果判断字符串类型的,则就要if(typeof ele.attr=='string')这样了,其它类型同理
    第二个参数可选,如果有第二个参数,则是设置文本值
    */
    function text(ele,str){
    if(ele&&ele.nodeType&&ele.nodeType===1){//如果第一个参数是元素类型
    if(str===undefined){//如果第二个参数没有传过来
    if(typeof ele.textContent=='string')
    //上句是在判断浏览器是不是支持textContent这个属性,
    //如果支持则此属性的类型为string,否则为undefined
    return ele.textContent;
    else
    return ele.innerText;
     
    }else if(typeof str=='string'){
    //如果传了第二个参数,并且第二个参数的类型正确,则是
    //给此元素设置文本值
    if(typeof ele.textContent=='string')
    ele.textContent=str;
    else
    ele.innerText=str;
    }else {
    alert('第二个参数str有误') ;
    throw new Error('第二个参数str有误');
    }
    }else{
    alert('第一个参数ele误!');
    throw new Error('第二个参数str有误');//这样写更好
    }
     
    }
  • 相关阅读:
    python mysql and ORM
    mysql-8.0.12-winx64 解压版安装(转)
    mysql装完计算机管理里面没mysql服务怎么解决(转)
    Python使用MySQL数据库(新)(转)
    Python之路,Day9
    python随笔2(列表的增删改查)
    python随笔1
    2018-05-23——PYTHON第三天
    2018-05-22——PYTHON第二天
    2018-05-21——python第一天
  • 原文地址:https://www.cnblogs.com/sheting/p/3968262.html
Copyright © 2020-2023  润新知