开发者

How can we find the childnode element type in javascript?

开发者 https://www.devze.com 2023-03-09 03:04 出处:网络
I have a table cell and I would like to know if it has a text box inside or simply the span tag in it 开发者_JAVA技巧dynamically using javascrip? If you want to check whether there\'s an <input>

I have a table cell and I would like to know if it has a text box inside or simply the span tag in it 开发者_JAVA技巧dynamically using javascrip?


If you want to check whether there's an <input> anywhere inside an element, you could use getElementsByTagName():

if (myTableCell.getElementsByTagName('input').length>=1) {
    ...do something with the input...
}


You can check the tagName attribute

function isInput(el){
  return /input/i.test(el.tagName);
}

or more generic:

function isElType(el,tagname){
   return RegExp(tagname,'i').test(el.tagName);
}
//usage
var isInput = isElType(myElement,'input');


Maybe something similar to this:

cell = document.getElementById('tableCell_ID');

spans = cell.getElementsByTagName( "span" );
0

精彩评论

暂无评论...
验证码 换一张
取 消