简体   繁体   English

从选择框中选择一个选项时,需要使用php动态更改标签或标题的文本颜色吗?

[英]When selecting an option from a select box, need to dynamically change text colour of label or header using php?

here is some of the sample code im working on. 这是一些正在使用的示例代码。 I just started learning php this year and am trying to experiment a little bit. 我今年刚开始学习php,现在正尝试做一些尝试。 so i have 所以我有

<select class="animal">
    <option value="dog"> Dog </option>
    <option value="cat"> Cat </option>
    <option value="cat"> Fish </option>
</select>

i want to change the background colour of the page in a different colour when choosing some option. 我想在选择某些选项时将页面的背景色更改为其他颜色。 For example, dog = blue background, cat = red background and fish = black background 例如,狗=蓝色背景,猫=红色背景,鱼=黑色背景

anyone has any idea how this would work? 任何人都不知道这将如何工作?

This can be achieved with the onclick attribrute 这可以通过onclick属性来实现

<select class="animal">
    <option value="dog" onclick="document.body.style.background = 'blue'"> Dog </option>
    <option value="cat" onclick="document.body.style.background = 'red'"> Cat </option>
    <option value="fish" onclick="document.body.style.background = 'black'"> Fish </option>
</select>

Using jQuery: 使用jQuery:

$(document).ready(function () {
    $('.animal').on('change', function () {
        $('body').css('background-color', $(this).find('option:selected').attr('data-color'));
    })
});

<select class="animal">
    <option value="dog" data-color="blue">Dog</option>
    <option value="cat" data-color="red">Cat</option>
    <option value="cat" data-color="green">Fish</option>
</select>

Demo: http://jsfiddle.net/lotusgodkk/GCu2D/121/ 演示: http//jsfiddle.net/lotusgodkk/GCu2D/121/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM