我正在尝试将html / css与我的jquery一起使用,但无法使其正常工作。
从现在开始,当我单击与“ vlabe”匹配的div时,它将根据状态打开或关闭。但是,我试图使用来自proto.io的html / css开/关开关,但我无法使其正常工作。
截至目前,如果我有<div id='light'></div>
,它将根据状态显示“开”或“关”。我需要将其添加/删除“已选中”到<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="" checked>
switchclick='';
if (vdata == 'Off' ) {
switchclick = 'onclick="SwitchToggle('+item.idx+', \'On\');"';
vdata = 'Off';
} else {
switchclick = 'onclick="SwitchToggle('+item.idx+', \'Off\');"';
vdata = 'On';
}
if( SwitchType == 'On/Off') {
// code to be executed if condition is true
$('#'+vlabel).html('<div '+switchclick+' >'+vdata+'</div>');
}
我希望我能解释自己和我想做的事情,否则请告诉我,不要对我投反对票。我在尝试。 :)
完整脚本供参考。
function RefreshData()
{
clearInterval($.refreshTimer);
var jurl=$.domoticzurl+"/json.htm?type=devices&plan="+$.roomplan+"&jsoncallback=?";
$.getJSON(jurl,
{
format: "json"
},
function(data) {
if (typeof data.result != 'undefined') {
$.each(data.result, function(i,item){
for( var ii = 0, len = $.PageArray.length; ii < len; ii++ ) {
if( $.PageArray[ii][0] === item.idx ) { // Domoticz idx number
var vtype= $.PageArray[ii][1]; // Domotitcz type (like Temp, Humidity)
var vlabel= $.PageArray[ii][2]; // cell number from HTML layout
var vdesc= $.PageArray[ii][3]; // description
var vattr= $.PageArray[ii][4]; // extra css attributes
var valarm= $.PageArray[ii][5]; // alarm value to turn text to red
var SwitchType= item.SwitchType; // Switch type "Dimmer""On/Off" "ECT"
var vdata= item[vtype]; // current value
//For Dimmers, if a Dimmer is "off it shows off, If on it shows the percent with a %"
if (item.Status == 'Off'){
DimmerLVL = 'Off';
} else {
DimmerLVL = (+item.Level+ '%');
}
// create switchable value when item is switch
switchclick='';
if (vdata == 'Off' ) {
switchclick = 'onclick="SwitchToggle('+item.idx+', \'On\');"';
vdata = 'Off';
} else {
switchclick = 'onclick="SwitchToggle('+item.idx+', \'Off\');"';
vdata = 'On';
}
if( SwitchType == 'On/Off') {
// code to be executed if condition is true
$('#'+vlabel).html('<div '+switchclick+' >'+vdata+'</div>');
} else if (SwitchType == 'Dimmer') {
// code to be executed if condition is false
$('#'+vlabel).html('<div>'+DimmerLVL+'</div>');
} else {
// code to be executed if condition is false
$('#'+vlabel).html('<div>123'+vdata+'</div>');
}
// scene alle verlichting uit: http://192.168.0.100:8080/json.htm?type=command¶m=switchscene&idx=2&switchcmd=ON
// if extra css attributes. Make switch not switchable when it is protected.
$('#desc_'+vlabel).html(vdesc);
}
}
});
}
});
$.refreshTimer = setInterval(RefreshData, 8000);
}
$(document).ready(function() {
$.roomplan=2; // define roomplan in Domoticz and create items below.
$.domoticzurl="http://192.168.1.125:8080";
//format: idx, value, label, description, [override css], [alarm value]
$.PageArray = [
['4', 'Status', 'cell6', 'Lamp',],
['2', 'Status', 'cell7', 'TV',],
];
RefreshData();
});
function SwitchToggle(idx, switchcmd)
{
console.log('function called');
$.ajax({
url: "http://192.168.1.125:8080/json.htm?type=command¶m=switchlight" + "&idx=" + idx + "&switchcmd=" + switchcmd + "&level=Level",
async: false,
dataType: 'json',
success: function(){
console.log('SUCCES');
},
error: function(){
console.log('ERROR');
}
});
RefreshData();
}
最佳答案
这对您有用吗?
$('#light').on('click', function() {
var state = $('#light').html();
var $checkbox = $('.onoffswitch-checkbox');
$('#light').html(state == 'Off' ? 'On' : 'Off');
$checkbox.prop('checked', !$checkbox.prop('checked'));
})
#light {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The checkbox:
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="" checked>
<br>
<br>Click the red div, The switch is:
<div id="light">On</div>
关于javascript - 如何在jQuery中添加html开关?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30584150/