获取元素的文本内容getText
api的应用场景是:获取指定的element的文本内容
/* 获取dom元素 @param {String|HTMLElement} id或者DOM元素 @info 对于ZYC.dom.g这个api在判断上更简单 只是判断了参数是否为字符串,如果不是直接返回 */ ZYC.dom._g = function(id){ if(ZYC.lang.isString(id)){ return document.getElementById(id); } return id; }
/*getText-get the textcontent from the element* *@function* *@param {string||HTMLElement} element* *@return {String} result *@relay on ZYC.dom.g* */ ZYC.dom.getText = function(element){ var result = ''; element = ZYC.dom._g(element); if(element.nodeType == 3 || element.nodeType == 4){ //text|CDATA result += element.nodeValue; }else if(element.nodeType !== 8){ var childs = element.childNodes; for(var i=0;i<childs.length;i++){ result += ZYC.dom.getText(childs[i]); } } return result; }
相关推荐
wikiwater 2020-10-27
IdeaElements 2020-08-19
Sophiego 2020-08-16
Kakoola 2020-08-01
Kakoola 2020-07-29
ELEMENTS爱乐冬雨 2020-07-18
ELEMENTS爱乐小超 2020-07-04
ELEMENTS爱乐小超 2020-07-04
Kakoola 2020-06-28
Feastaw 2020-06-18
Wmeng0 2020-06-14
ELEMENTS爱乐冬雨 2020-06-14
云之高水之远 2020-06-14
哈喽elements 2020-06-14
Feastaw 2020-06-11