更改文本颜色onclick [英] Changing text color onclick

查看:88
本文介绍了更改文本颜色onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaScript的新手。我使用JavaScript开发了一个页面,当我选择一种颜色时,它会作为背景应用于整个页面。

I'm new to JavaScript. I've developed a page using JavaScript in such a way that when I select a color it is applied to the whole page as a background.

我想开发一个只能更改文字颜色的页面。它应该被更改(从红色到绿色或类似的东西),但页面不应该刷新,只应更改选定的内容或文本颜色。

I want to develop a page where I can change only the text color. It should get changed (from red to green or something like that), but the page shouldn't get refreshed, and only the selected contents or text color should be changed.

任何人都可以帮助我。有关如何开发它的任何想法?在此先感谢。

Can any one please help me in this. Any ideas of how to develop it? Thanks in advance.

推荐答案

Sarfraz重写的答案就是这样的,我想:

A rewrite of the answer by Sarfraz would be something like this, I think:

<script>

    document.getElementById('change').onclick = changeColor;   

    function changeColor() {
        document.body.style.color = "purple";
        return false;
    }   

</script>

您必须将此脚本放在页面底部,就在结束机构之前标记,或将处理程序赋值放在一个名为onload的函数中 - 或者如果你使用的是jQuery,那就是非常优雅的 $(document).ready(function(){...});

You'd either have to put this script at the bottom of your page, right before the closing body tag, or put the handler assignment in a function called onload - or if you're using jQuery there's the very elegant $(document).ready(function() { ... } );

请注意,当您以这种方式分配事件处理程序时,它会从HTML中获取功能。另请注意,您将其设置为等于函数名称 - 否()。如果您执行 onclick = myFunc(); 该函数将在设置处理程序时实际执行。

Note that when you assign event handlers this way, it takes the functionality out of your HTML. Also note you set it equal to the function name -- no (). If you did onclick = myFunc(); the function would actually execute when the handler is being set.

和我很好奇 - 你知道脚本改变背景颜色,但不是文字颜色吗?奇怪:))

And I'm curious -- you knew enough to script changing the background color, but not the text color? strange:)

这篇关于更改文本颜色onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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