$("#div2").html(arr[0])我这句都可以把变量显示在页面上了, 为何我下面哪个背景颜色的if 没有触发?
function ddiv(id){
var oDiv=document.getElementById(id);
if(oDiv.style.backgroundColor!='rgb(255, 0, 102)') //如果把这里和下面的两个颜色值互换一下
{
oDiv.style.backgroundColor='#FF0066'; //是可以变红的
}
else
{
oDiv.style.backgroundColor='#E1E1E1'; //但是再点一下就不会变回黑色了
}
}
function get_txm(){
// var name = $('input[name="username"]').val();
// alert(name);
var txmid = document.getElementById("txm").value;
$.ajax({
url:"http://localhost/admin/129.asp",
type: "POST",
data: {
id: txmid,
},
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success:function(res){
$("#div1").html(res);//在DIV里显示
if (res) {
var arr = res.split('|');
$("#div2").html(arr[0]);//在DIV里显示
var oDiv=arr[0]
if(oDiv.style.backgroundColor!='rgb(255, 0, 102)') //如果把这里和下面的两个颜色值互换一下
{
oDiv.style.backgroundColor='#FF0066'; //是可以变红的
}
else
{
oDiv.style.backgroundColor='#E1E1E1'; //但是再点一下就不会变回黑色了
}
}
}
})
}
【以下回答由 GPT 生成】
这段代码中存在一个问题,导致第二个地方的背景颜色始终无法改为 #E1E1E1
。问题出在以下代码块中:
if (oDiv.style.backgroundColor != 'rgb(255, 0, 102)') {
oDiv.style.backgroundColor = '#FF0066';
} else {
oDiv.style.backgroundColor = '#E1E1E1';
}
oDiv
在这里是一个字符串,并不是一个DOM元素,所以 oDiv.style.backgroundColor
没有实际效果。为了解决这个问题,我们可以稍作修改,让 oDiv
成为一个DOM元素,代码如下所示:
if (oDiv.style.backgroundColor !== 'rgb(255, 0, 102)') {
oDiv = document.getElementById('div2'); // 获取实际的DOM元素
oDiv.style.backgroundColor = '#FF0066';
} else {
oDiv.style.backgroundColor = '#E1E1E1';
}
这样的话,当 res
的值满足条件时,将会获取到实际的DOM元素,并正确地改变其背景颜色。完整的修改代码为:
function ddiv(id) {
var oDiv = document.getElementById(id);
if (oDiv.style.backgroundColor != 'rgb(255, 0, 102)') {
oDiv.style.backgroundColor = '#FF0066';
} else {
oDiv.style.backgroundColor = '#E1E1E1';
}
}
function get_txm() {
var txmid = document.getElementById("txm").value;
$.ajax({
url: "http://localhost/admin/129.asp",
type: "POST",
data: {
id: txmid,
},
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success: function(res) {
$("#div1").html(res);
if (res) {
var arr = res.split('|');
$("#div2").html(arr[0]);
var oDiv = document.getElementById('div2');
if (oDiv.style.backgroundColor != 'rgb(255, 0, 102)') {
oDiv.style.backgroundColor = '#FF0066';
} else {
oDiv.style.backgroundColor = '#E1E1E1';
}
}
}
});
}
以上就是对代码的优化和解决这个问题的具体方案。如果还有其他问题,请随时提问。
【相关推荐】
你的问题看起来是在于ajax请求成功后,你尝试调用ddiv(arr[0])
函数,但是这个函数并没有被触发。
这可能是因为你的ajax请求是异步的,所以当你在ajax的success回调中调用ddiv(arr[0])
时,可能这个函数还没有得到执行,就已经到了ajax请求的结束。
你可以尝试将ddiv(arr[0]);
这行代码放在if (res) {...}
语句块的外面,确保在ajax请求成功返回结果后,无论这个结果是否存在,ddiv(arr[0]);
都会被尝试调用。
下面是修改后的代码:
function get_txm(){
var txmid = document.getElementById("txm").value;
$.ajax({
url:"http://localhost/admin/129.asp",
type: "POST",
data: {
id: txmid,
},
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success:function(res){
$("#div1").html(res);//在DIV里显示
if (res) {
var arr = res.split('|');
$("#div2").html(arr[0]);//在DIV里显示
}
ddiv(arr[0]);
}
})
}
但是请注意,这个解决方案假设无论何时res是否有值,你的ddiv()
函数都能正常工作。如果你的函数有对res
的特定检查或者需要特定的上下文环境才能正确运行,那么你可能需要对你的代码进行更深入的修改。
这是用了jquery的内容吗?可以试试prop有作用不