我正试图获得一个if/ get语句来工作,但是我遗漏了一些东西。
从我的API中,我被提取了一个JSON对象“实体id”和id " state“的状态。
创建实体状态:
var $$ = Dom7;
$$('[entity-status="' + entity.entity_id + '"]').html(entity.state);上面的代码可以在我的HTML中使用,并将正确地显示状态。<div entity-status='switch.office_light_2'></div> DIV将显示ON
当我试图在if/ not语句中使用它时,它不起作用。
if ($("entity-status='switch.office_light_2'") === "ON") {
document.getElementById("live").innerHTML = "Success";
} else {
document.getElementById("live").innerHTML = "nope";
}我怎么才能让这个起作用?
发布于 2017-09-05 04:11:09
要选择div with属性,可以使用:
$("div[entity-status='switch.office_light_2']")
$(document).on('click', '#bn_Switch', function() {
//attribute selector
var statusDiv = $("div[entity-status='switch.office_light_2']");
//getting current text of div to change the status
var statusText = statusDiv.text() == "ON" ? "OFF" : "ON";
$(statusDiv).text(statusText);
//checking with the changed text to update 'live' div
document.getElementById("live").innerHTML = (statusDiv.text() == "ON" ? "Success" : "nope");
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div entity-status='switch.office_light_2'>
</div>
<input id="bn_Switch" type="submit" value="Click" />
<div id='live'>
</div>
https://stackoverflow.com/questions/46041812
复制相似问题