代码可以简化吗? [英] Can the code be simplified?

查看:56
本文介绍了代码可以简化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下页面:

http: //www.pinyinology.com/test/testGet.html


部分代码为:


javascript

document.getElementById(" moon1")。innerHTML =" moon";

document.getElementById(" moon2")。innerHTML =" moon";

document.getElementById(" moon3")。innerHTML =" moon";

document.getElementById(" moon4")。innerHTML =" moon";


html:


< p id =" moon1">< / p>

< p id =" moon2">< / p>

< p id =" moon3">< / p>

< p id =" moon4">< / p>


我想知道代码是否可以简化,例如< class =''moon ''在
中的html部分,以及javascript部分中的getElementsByClass。

感谢您的专业知识。


Fulio Pen

解决方案

fulio pen写道:


我有以下页面:

http://www.pinyinology.com/test/testGet.html


部分代码是:


javascript

document.getElementById(" moon1")。innerHTML =" moon";

document.getElementById(" moon2")。innerHTML =" moon";

document.getElementById(" moon3")。innerHTML =" moon";

document.getElementById(" moon4")。innerHTML =" moon";


html:


< p id =" moon1">< / p>

< p id =" moon2">< / p>

< p id =" ; moon3">< / p>

< p id =" moon4">< / p&g t;


我想知道代码是否可以简化,例如< class =''moon''in

html部分和getElementsByClass in javascript部分。

感谢您的专业知识。



我不想将类用于任意目的。相反,我创建了自己的

属性:


HTML:

< p moon>< / p>

< p moon>< / p>

....


javascript

var moons = document.getElementsByAttribute(" moon");

for(var m = 0; m!= moons.length; ++ m)

moons [m] .innerHTML =" moon";


当然,document.getElementsByAttribute不是标准函数,而是

简单的谷歌搜索将产生许多实现。


-

Josh


9月18,10:31 * pm,Josh Sebastian< sebas ... @ gmail.comwrote:


fulio pen写道:


我有以下页面:

http://www.pinyinology.com/test/testGet.html


部分代码是:


javascript

* document.getElementById(" moon1")。innerHTML =" moon";

document.getElementById(" moon2")。innerHTML =" moon" ;;

document.getElementById(" moon3")。innerHTML =" moon";

document.getElementById(" moon4")。innerHTML =" moon" ;;


html:


< p id =" moon1" ;>< / p>

< p id =" moon2">< / p>

< p id =" moon3"> ;< / p>

< p id =" moon4">< / p>


我想知道代码是否可以简化,例如< class =''moon''

html部分和javascript部分中的getElementsByClass。

感谢您的专业知识。



我不想将类用于任意目的。相反,我创建了自己的

属性:


HTML:

< p moon>< / p>

< p moon>< / p>

...


javascript

var moons = document.getElementsByAttribute(" moon");

for(var m = 0; m!= moons.length; ++ m)

* moons [m] .innerHTML =" moon";


当然,document.getElementsByAttribute不是标准函数,而是

简单的谷歌搜索将产生许多实现。


-

Josh



Josh :


非常感谢你的帮助。


Fulio Pen


Josh Sebastian写道:


fulio pen写道:


>< p id =" moon1" >< / p>
< p id =" moon2">< / p>
< p id =" moon3">< / p>
< p id =" moon4">< / p>

我想知道代码是否可以简化,例如html部分中的< class =''moon'',以及javascript部分中的getElementsByClass。
感谢您的专业知识。



在档案中搜索getElementsByClass。不客气。


< http://jibbering.com/faq/>


我不喜欢将类用于任意目的。相反,我创建了自己的

属性:


HTML:



所以你更喜欢*无效代码*超过定义的微格式?


< p moon>< / p>

< p moon>< / p> ;

...



这是垃圾,而不是HTML。


< http ://validator.w3.org/>

PointedEars

-

Prototype.js是由不爱的人写的知道javascript for people

谁不懂javascript。不知道javascript的人不是设计使用javascript的系统的最佳建议来源。

- Richard Cornford,cljs,< f8 ** *****************@news.demon.co.uk>


I have following page:

http://www.pinyinology.com/test/testGet.html

Part of the code is:

javascript:
document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";

html:

<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class=''moon''in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.

Fulio Pen

解决方案

fulio pen wrote:

I have following page:

http://www.pinyinology.com/test/testGet.html

Part of the code is:

javascript:
document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";

html:

<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class=''moon''in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.

I prefer not to use classes for arbitrary purposes. Rather, I create my own
attribute:

HTML:
<p moon></p>
<p moon></p>
....

javascript:
var moons = document.getElementsByAttribute("moon");
for(var m=0; m != moons.length; ++m)
moons[m].innerHTML = "moon";

Of course, document.getElementsByAttribute is not a standard function, but a
simple Google search will yield a number of implementations.

--
Josh


On Sep 18, 10:31*pm, Josh Sebastian <sebas...@gmail.comwrote:

fulio pen wrote:

I have following page:

http://www.pinyinology.com/test/testGet.html

Part of the code is:

javascript:
*document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";

html:

<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class=''moon''in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.


I prefer not to use classes for arbitrary purposes. Rather, I create my own
attribute:

HTML:
<p moon></p>
<p moon></p>
...

javascript:
var moons = document.getElementsByAttribute("moon");
for(var m=0; m != moons.length; ++m)
* moons[m].innerHTML = "moon";

Of course, document.getElementsByAttribute is not a standard function, but a
simple Google search will yield a number of implementations.

--
Josh

Josh:

Thanks a lot for your help.

Fulio Pen


Josh Sebastian wrote:

fulio pen wrote:

><p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class=''moon''in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.

Search the archives for "getElementsByClass". You are welcome.

<http://jibbering.com/faq/>

I prefer not to use classes for arbitrary purposes. Rather, I create my own
attribute:

HTML:

So you prefer *invalid code* over defined microformats?

<p moon></p>
<p moon></p>
...

This is junk, not HTML.

<http://validator.w3.org/>
PointedEars
--
Prototype.js was written by people who don''t know javascript for people
who don''t know javascript. People who don''t know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>


这篇关于代码可以简化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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