【问题标题】:How to select text on click on text in html? [duplicate]如何在单击html中的文本时选择文本? [复制]
【发布时间】:2021-11-29 10:15:27
【问题描述】:

我想在单击颜色时选择它们并复制它们。 它们在 html 中定义如下:

<span>Colour<span>

我是编程初学者,请帮忙!

【问题讨论】:

  • 请添加相关的 HTML 并尝试解释"I want when clicked on colours to select them and copy them" ~ 哪些颜色?在哪里?它们是否来自上图所建议的input 元素?

标签: javascript html select copy-paste


【解决方案1】:

您的问题并不清楚您想做什么。据我了解,您需要通过输入十六进制值来更改颜色,这可以通过 DOM 操作来完成。下面是一个在按钮点击时改变颜色的基本代码。

<html>
<head>
<script>
    var colors = ["red", "blue", "green"];
    var colorIndex = 0;
    function changeColor() {
        var col = document.getElementById("body");
        if( colorIndex >= colors.length ) {
            colorIndex = 0;
        }
        col.style.backgroundColor = colors[colorIndex];
        colorIndex++;
    }
</script>
    <body id='body'>
    <button onclick="changeColor();">Change color</button>
    </body>
</html>

同样,您可以只使用输入框中输入的十六进制代码并相应地设置背景/线条颜色。您将需要 js 和 HTML 的帮助,并根据需要使用 if 或 classes。如果有帮助,请点赞。提前谢谢。

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 2019-12-12
    • 2020-06-18
    • 1970-01-01
    • 2015-10-19
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多