Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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 单击元素时如何更改文本颜色_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 单击元素时如何更改文本颜色

Javascript 单击元素时如何更改文本颜色,javascript,jquery,html,css,Javascript,Jquery,Html,Css,注意:我没有写所有的网页代码,所以我不确定什么是pdiv,我只是想尝试一下我是否可以更改这个小问题[单击时要更改文本颜色以指示您正在阅读的部分] 这一部分是这样的: function changeColor(pdiv){ //get all divs and remove read class. var divs = document.getElementsByClassName('read'); for(var div in divs){ div.cla

注意:我没有写所有的网页代码,所以我不确定什么是
pdiv
,我只是想尝试一下我是否可以更改这个小问题[单击时要更改文本颜色以指示您正在阅读的部分]

这一部分是这样的:

function changeColor(pdiv){
    //get all divs and remove read class.
    var divs = document.getElementsByClassName('read');
    for(var div in divs){
        div.classList.remove('read');
    }
    //add read class to current div.
    pdiv.classList.add('read');
}

然后,当您单击时,它将显示所有菜单部分:

函数menuShowPol(pdiv){
if(document.getElementById('menu_pol_div'+pdiv)。
style.display==“无”){
document.getElementById('menu_pol_div'+pdiv)
.style.display=“内联块”;
}否则{
document.getElementById('menu_pol_div'+pdiv)
.style.display=“无”;
}		
}
功能更改颜色(pdiv){
{
document.getElementById('menu_pol_div'+pdiv)
.style.color=“#ff0000”//forecolor
document.getElementById('menu_pol_div'+pdiv)
.style.backgroundColor=“#ff0000”//backcolor
}
if($showpol==1){
$menutext.=''.$thispol.\n“
在我看来,我认为 如果你试着按按钮

$menutext .= "<div id=\"country_menu_report_name\" onclick=\"menuShowPol('".$pol."');changeColor('".$pol."'); return false;\">".$thispol."</div>\n";
您需要在此行中查看相同的id:

document.getElementById('country_menu_report_name\')
或 $menutext.=''.$thispol.\n“

看看“pvid”在ID:example中

为了


我有两个css类:

.read{
   color: red;
}

.unread {
   color: blue;
}
然后,我会这样做:

function changeColor(pdiv){
    //get all divs and remove read class.
    var divs = document.getElementsByClassName('read');
    for(var div in divs){
        div.classList.remove('read');
    }
    //add read class to current div.
    pdiv.classList.add('read');
}
实际上,您可以将“this”传递给您的事件,这样可以直接访问与之交互的元素

注意页眉divs中的
onclick=“changeColor(this)”
。此外,我还将国家/地区菜单报告链接到了菜单列表,这不是最好的做法,但它很管用,而且很普通。
功能更改颜色(pdiv){
var myid=pdiv.id;
var otherid=myid.replace(“国家/地区/菜单/报告”,“菜单/政策”);
var otherdiv=document.getElementById(otherid);
var toggle=otherdiv.style.display==“无”;
如果(!切换){
pdiv.style.color=“#0000ff”//前景色
pdiv.style.backgroundColor=“#ccccff”//backcolor
otherdiv.style.display=“无”;
}否则{
pdiv.style.color=“#000000”//前景色
pdiv.style.backgroundColor=“#aaeeaa”//背景色
otherdiv.style.backgroundColor=“#ccffcc”//backcolor
otherdiv.style.display=“块”;
}
}
测试分区A.单击我
菜单项A
测试分区B。单击我

菜单项B
请将您的php代码更改为输出html,以便我们能够实际看到问题。。
.read{
   color: red;
}

.unread {
   color: blue;
}
function changeColor(pdiv){
    //get all divs and remove read class.
    var divs = document.getElementsByClassName('read');
    for(var div in divs){
        div.classList.remove('read');
    }
    //add read class to current div.
    pdiv.classList.add('read');
}