开发者

javascript and div ids

开发者 https://www.devze.com 2023-01-03 15:42 出处:网络
If i have the following code in my html file <div id=div1开发者_C百科> <script type=\"text/javascript\">

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.
}
0

精彩评论

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

关注公众号