How to get Element Children Attributes with JQuery From Passed in Element
I have a开发者_开发技巧 function that takes an element as a parameter and from there I need to get attribute info from one of the child elements.
function getTablesForSchema(element) {
alert(element);
var schemaName = $(element).children('ul:first').attr('id');
alert(schemaName);
...
The alert displays [object HTMLLIElement] so I know the element parameter is correct. The 'li' element has a couple embeded 'ul' elements inside of it and I need to get the first one and it's 'id' attribute.
If I do an alert on the element like so the alert fails. I'm at a loss here. Any ideas?
var tempElement = $(element);
alert(tempElement);
var schemaName = $(element).children('ul:first').attr('id');
The above code should work if the 'ul' element is a direct child of 'element'. Otherwise you may want to use this instead:
var schemaName = $(element).find('ul:first').attr('id');
var schemaName = $(element).children('ul:first').get(0).id;
Have you tried using JavaScript console in Firefox (with Firebug) or in Chrome? They have console.log(element)
. A bit better tool too for debugging than alert.
精彩评论