DIV的ID中的写入值的Javascript代码 [英] Javascript Code for Write Value inside the ID of DIV

查看:50
本文介绍了DIV的ID中的写入值的Javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我有Div Tag,我需要在DIV TAG中动态写入ID.这是下面的示例代码,



I have Div Tag and i need to write the ID dynamically in DIV TAG. Here is the below sample code,

<div class = "popupButton" id =" "  önclick="Processing();">Processing</div>



是否有任何JavaScript代码可在DIV的"ID"中写入div ID


谢谢

Dileep



Is there any javascript code to write div id inside the "ID" of DIV


Thanks

Dileep

推荐答案

做到这一点-只需根据需要设置元素的.id属性.
请记住,您仍然需要以某种方式访问​​div的方法.在这里,我
通过(a)用id将容器中的目标围起来(b)假定该容器中的所有元素都是目标,只要它们是div,就可以做到这一点.您会注意到第一个子div的ID被覆盖,其余3个是第一次分配ID.

This will do it - just set the element''s .id property as needed.
Bear in mind you''ll still need a way of accessing the div somehow. Here, I''ve
done it by (a) enclosing the targets in a container with an id (b) assuming that all elements in that container are a target so long as they are a div. You''ll notice the first child div''s id is overwritten and the remaining 3 are given an id for the 1st time.

lt;head>
<script>
function setIDs(parentIdStr, tagName)
{
  var parent = document.getElementById(parentIdStr);
  var nodes = parent.getElementsByTagName(tagName);
  
  var numNodes = nodes.length;
  var i;
  alert(numNodes);
  for (i=0; i<numNodes; i++)
  {
    var curNode = nodes[i];
    curNode.id = 'itemId'+(i+1);
  }
}
</script>
</head>

<body onload='setIDs("container", "div");'>
<div id='container'>
 <div id='div1'>1st div</div>
 <div>2nd div</div>
 <div>3rd div</div>
 <div>4th div</div>
</div>
<body>
</html>


如果我正确地跟随了您,则需要为已创建的div动态分配一些ID.

每当您声明不带id的div时,都不会直接通过id对其进行标识.
好吧,这并不意味着javascript无法识别它.
您可以获取所有元素并进行迭代以获取div对象.
但是问题是,可以有多个没有id的div.因此,尽管可以获取div对象,但无法确定哪个对象.

我想,您可以使用属性来满足您的需求.
或者,当您生成aspx页面时,请在fy上创建html并将其设置在div对象中.因此,您可以在生成html时知道ID.

这是使用jquery将ID分配给div的解决方案,
If I follow you correctly, you need to assign some id dynamically to a div already created.

Whenever you declare a div without id, it will not be identified directly through id.
Well that doesn''t mean, that javascript cannot identify it.
You can getallelements and iterate to get the div object.
But the problem is, there can be multiple div without id. So, though you can get the div object you cannot identify which one is which.

I presume, you use attribute to cater you requirements.
Or when you generate the aspx page, create the html on fy and set it in div object. Thus you can know the id while generating the html.

Here is a solution to assign id to div using jquery,
var idCount = 1;


(``div'').each(function(){
(''div'').each(function() {


这篇关于DIV的ID中的写入值的Javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆