设置< DIV>的inner.HTML。有重复的ID ... [英] setting the inner.HTML of <DIV> with duplicate IDs...

查看:70
本文介绍了设置< DIV>的inner.HTML。有重复的ID ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


有什么方法可以让它发挥作用吗?


myID.innerHTML =" Hello"


< DIV id =" myID">< / DIV>

< DIV id =" myID">< / DIV>

我想要你好在每个DIV中使用ID为myID的apear。我这样做现在想要改变身份证。$ / b
$ b提前致谢!

Hello,

is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
I would like "Hello" to apear in each DIV with the ID of ''myID''. I do
now want to change the id.

Thanks in advance!

推荐答案

" Charlie T" < CH ************* @ gmail.com>在消息中写道

新闻:ee ************************** @ posting.google.c om ...
"Charlie T" <ch*************@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
您好,

有什么方法可以让它工作吗?

myID.innerHTML =" Hello"

< DIV id =" myID">< / DIV>
< DIV id =" myID">< / DIV>

我想要& ;你好"在每个DIV中使用ID为myID的apear。我现在想改变id。
Hello,

is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
I would like "Hello" to apear in each DIV with the ID of ''myID''. I do
now want to change the id.




不要!


id属性是应该是独一无二的。要在

上有重复的ID,同一页面实际上错误地使用了id-tag。


请参阅: http://www.w3.org/TR/html4/struct/global .html#adef-id


如果你必须有相同的名字,请使用name属性。


-

Dag

58°26''15.9" N 008°46''45.5 E



Don''t!

The id attribute is supposed to be unique. To have duplicate id''s on
the same page is actually wrong use of the id-tag.

see: http://www.w3.org/TR/html4/struct/global.html#adef-id

Use the name attribute if you must have equal names.

--
Dag
58°26''15.9" N 008°46''45.5" E


2004年9月8日星期三05:43:14 GMT,Dag Sunde< ds@orion.no-way>写道:
On Wed, 08 Sep 2004 05:43:14 GMT, Dag Sunde <ds@orion.no-way> wrote:
" Charlie T" < CH ************* @ gmail.com>在消息中写道
新闻:ee ************************** @ posting.google.c om ...
"Charlie T" <ch*************@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
有什么方法可以让它工作吗?

myID.innerHTML =" Hello"

< DIV id =" myID" ;>< / DIV>
< DIV id =" myID">< / DIV>


不,没有。正如Dag所说,使用相同的ID是无效的HTML,所以

浏览器行为可能无法预测。

我想要Hello在每个DIV中使用ID为myID的apear。我现在想要改变身份证。
is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
No, there isn''t. As Dag said, using the same ID is invalid HTML and so
browser behaviour can be unpredictable.
I would like "Hello" to apear in each DIV with the ID of ''myID''. I do
now want to change the id.



你必须改变它们。一旦你这样做,你可以使用类似的东西:


< div id =" myID1">& nbsp;< / div>

< div id =" myID2">& nbsp;< / div>


var getRefById = function(){return null;};

if(document.getElementById){

getRefById = function(i){return document.getElementById(i);};

} else if(document.all) {

getRefById = function(i){return document.all [i] || null;};

}


函数changeContents(){

var o,nD = 2; // DIV的数量


for(var i = 1; i< = nD; ++ i){

o = getRefById(''myID' '+ i);


if(o){o.innerHTML =''Hello'';}

}

}


[snip]

如果你必须有相同的名字,请使用name属性。


You''ll have to alter them. Once you do, you can use something like:

<div id="myID1">&nbsp;</div>
<div id="myID2">&nbsp;</div>

var getRefById = function() {return null;};
if(document.getElementById) {
getRefById = function(i) {return document.getElementById(i);};
} else if(document.all) {
getRefById = function(i) {return document.all[i] || null;};
}

function changeContents() {
var o, nD = 2; // Number of DIVs

for(var i = 1; i <= nD; ++i) {
o = getRefById(''myID'' + i);

if(o) {o.innerHTML = ''Hello'';}
}
}

[snip]
Use the name attribute if you must have equal names.




那没有比这更好的了。 DIV元素没有名称属性。


Mike


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。



That isn''t any better. The DIV element doesn''t have a name attribute.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


" Charlie T" < CH ************* @ gmail.com>在消息中写道

新闻:ee ************************** @ posting.google.c om ...
"Charlie T" <ch*************@gmail.com> wrote in message
news:ee**************************@posting.google.c om...
您好,

有什么方法可以让它工作吗?

myID.innerHTML =" Hello"

< DIV id =" myID">< / DIV>
< DIV id =" myID">< / DIV>

我想要& ;你好"在每个DIV中使用ID为myID的apear。我现在想要更改ID。
Hello,

is there any way to get this to work?

myID.innerHTML = "Hello"

<DIV id="myID"></DIV>
<DIV id="myID"></DIV>
I would like "Hello" to apear in each DIV with the ID of ''myID''. I do
now want to change the id.




ID *必须*在页面中是唯一的,因此您必须更改它们。


以下代码,我被告知,使用

类名称myclass循环遍历所有DIV元素。并将文本颜色更改为红色。


是否可以更改id =" myId" to class =" myclass"在你的页面上

然后改变下面的例程,这样它就会改变每个元素的innerHTML

在divs [i]中列出而不是文本颜色?


var divs = document.getElementsByTagName(''div'');

if(divs){

for(var i = 0; i< ; divs.length; i ++){

if(divs [i] .className ==''myclass''){

divs [i] .style.color = ''红色';

}

}

}



IDs *must* be unique within the page so you will have to change them.

The following code, I''ve been told, loops through all DIV elements with the
class name "myclass" and changes the text colour to red.

Would it be possible to change the id="myId" to class="myclass" on your page
then change the routine below so it changes the innerHTML of each element
listed in divs[i] instead of the text colour?

var divs = document.getElementsByTagName(''div'');
if (divs) {
for (var i = 0; i < divs.length; i++) {
if (divs[i].className == ''myclass'') {
divs[i].style.color = ''Red'';
}
}
}


这篇关于设置&lt; DIV&gt;的inner.HTML。有重复的ID ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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