If i have the following code in my html file
<div id=div1开发者_C百科>
<script type="text/javascript">
externalFunction();
</script>
Is it possible to get the id of the div in the method externalFunction()?
Regards Damien
You mean the id? Name is different. The short answer is no, the longer answer is you can if you know something about the div you are looking for ( such as the class or where it's located or anything else.)
If you are attempting to simply get the div
without actually referencing the id
you could do the following in jQuery:
$("div").each(function() {
// Do stuff.
});
This would essentially loop around each of the divs
on the page, it would then be up to your externalFunction()
to handle each div
accordingly.
And for when jQuery isn't an option:
var divElements = document.getElementsByTagName("div");
for (var i=0; i < divElements.length; i++) {
var currDiv = divElements[i]; // Do stuff with the element.
}
精彩评论