English 中文(简体)
Java字通行证参数,以防 lo功能
原标题:Javascript pass parameters to onclick function in loop

任何使这一文字发挥作用的想法?


I have two paragraphs:

<p data-color="red">This textarea has a character limit of 50.</p>
<p data-color="blue">This one has a character limit of 40.</p>​


And some JS:

<script>
var p = document.getElementsByTagName( p ),
    i = p.length;

while (i--) {
    color = p[i].getAttribute( data-color );
    p[i].onclick = function() {
        this.style.color = color;
    };
}​
</script>​

Demo

我想这些段落在被点击时转去其特定的颜色。 我需要将每个元素的颜色纳入浮标功能。

任何想法?

预言如此之多!

问题回答
<script>
var p = document.getElementsByTagName( p ),
    i = p.length;

while (i--) {
    p[i].onclick = function() {
        this.style.color = this.getAttribute( data-color );
    };
}​
</script>​

于是,我又一次开枪。 Pure JS. 这里是我来的。 正如有人指出的那样,将肤色作为变数,实际上没有任何办法。 因此,将一个类别加起来,使用这个类别作为颜色,似乎是避免 j笑和提供非常简单的解决办法的唯一途径。 由于示范法中没有任何段落可以开始,这似乎是一个可行的解决办法。 这里是我的支部。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签