js—关于innerText与TextContent的兼容代码

2019-03-22  本文已影响0人  inspg

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

</head>

<body>

<input type="button" name="btn" id="btn" value="点击按钮" />

<div id="div3">

哈喽

</div>

</body>

<script src="common.js">

</script>

<script type="text/javascript">

function setInnerText(element,text){

if(typeof element.innerText==undefined){

element.textContent=text;

}

else{

element.innerText=text;

}

}

function getInnerText(element){

if(element.innerText==undefined){

return element.textContent;}

else {return element.innerText;}

}

my$("btn").onclick=function(){

console.log(getInnerText(my$("div3")));

}

</script>

</html>

上一篇下一篇

猜你喜欢

热点阅读