Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript更改DIV背景色onclick_Javascript_Html_Css_Background Color - Fatal编程技术网

Javascript更改DIV背景色onclick

Javascript更改DIV背景色onclick,javascript,html,css,background-color,Javascript,Html,Css,Background Color,我正在尝试在提交表格后更改div的颜色。新页面上的div应为所选颜色 JS 函数更改(col){ col.style.backgroundColor=''; } HTML 使用jQuery,您可以轻松做到这一点: $("#yourID").click(function(){ $(this).css("background-color","yellow"); }); 最简单的方法是将颜色作为参数在两个页面之间传递,然后在第二个页面上使用jQuery将颜色设置为从该参数中获得的颜

我正在尝试在提交表格后更改div的颜色。新页面上的div应为所选颜色

JS

函数更改(col){
col.style.backgroundColor='';
}
HTML




使用jQuery,您可以轻松做到这一点:

$("#yourID").click(function(){

  $(this).css("background-color","yellow");

  });

最简单的方法是将颜色作为参数在两个页面之间传递,然后在第二个页面上使用jQuery将颜色设置为从该参数中获得的颜色。

请看这里:

代码

var colors = ["red", "blue", "yellow", "green", "orange", "black", "cyan", "magenta"]

function changeColor() {
    //you can as well pass col reference as you do in your code
    var col = document.getElementById("changecolor");
    col.style.backgroundColor = colors[Math.floor((Math.random()*8)+1)];
}
适应您的需要,希望对您有所帮助
<? $message="dd"; ?>
<script>
function change(col) {
document.getElementById('col').style.backgroundColor=col;
}
 </script>

<form action="" method="post">
<input type='search' name='keywords' value='' style="width:100%;">

<a href='#col'>
<input type=submit  value='Submit' name='doSearch' style="visibility: hidden;" onclick="enter(this.href)" />
 </a>
 </form>


<div style="width:50px; height:50px;" id="col" onclick="location.href='index2.php';" >

 <br/>
<?php echo $message ?>

</div>
<? if(isset($_POST['keywords'])){ ?>
<script>
 change('<?=$_POST['keywords']?>');
</script>
<? } ?>
功能更改(col){ document.getElementById('col').style.backgroundColor=col; }
变更(“”);
测试它,它通过在关键字输入上插入颜色来工作

调用的
change(col)
函数在哪里?在另一页?我们能得到另一页的代码吗?您在控制台中弹出任何错误吗?选择后,您指的是下拉菜单?调用了更改。此页面上存在所有内容。“我已经修好了。”本德尔说。它是从php函数中选择的。谢谢
var colors = ["red", "blue", "yellow", "green", "orange", "black", "cyan", "magenta"]

function changeColor() {
    //you can as well pass col reference as you do in your code
    var col = document.getElementById("changecolor");
    col.style.backgroundColor = colors[Math.floor((Math.random()*8)+1)];
}
<? $message="dd"; ?>
<script>
function change(col) {
document.getElementById('col').style.backgroundColor=col;
}
 </script>

<form action="" method="post">
<input type='search' name='keywords' value='' style="width:100%;">

<a href='#col'>
<input type=submit  value='Submit' name='doSearch' style="visibility: hidden;" onclick="enter(this.href)" />
 </a>
 </form>


<div style="width:50px; height:50px;" id="col" onclick="location.href='index2.php';" >

 <br/>
<?php echo $message ?>

</div>
<? if(isset($_POST['keywords'])){ ?>
<script>
 change('<?=$_POST['keywords']?>');
</script>
<? } ?>