zoukankan      html  css  js  c++  java
  • 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有误');//这样写更好
    }
     
    }
  • 相关阅读:
    hdu 4864 Task
    hdu 1501 Zipper
    hdu 1428 漫步校园
    hdu 1505 City Game
    hdu 1337 The Drunk Jailer
    9-13记录
    python 读取unicode编码文件
    梯度出现Nan值的追踪
    Rstudio-server更改R版本
    stdout/stderr作用学习
  • 原文地址:https://www.cnblogs.com/sheting/p/3968262.html
Copyright © 2011-2022 走看看