点击.class我想在#id上面执行orangeBarClick,否则我想显示orangeBar:
<div class="class">
Click this.
</div>
<div #spl-footer>
Insert before this setence.
</div>
var orangeBar = '<div>Default</div>';
var orangeBarClick = '<div>OnClick</div>';
if ($('.click').data('clicked')) {
$(orangeBarClick).insertBefore("#spl-footer");
} else {
$(orangeBar).insertBefore("#spl-footer");
});
我认为我没有正确编写if / else语句,因为它没有执行。我在这里错过了什么? https://jsfiddle.net/q0536q3h/
答案 0 :(得分:0)
# Please try again #
<div class="click">Click this.</div>
<div id="spl-footer">Insert before this setence.</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var orangeBar = '<div>Default</div>';
var orangeBarClick = '<div>OnClick</div>';
//var isClicked = jQuery('.click').data('clicked');
//alert(isClicked);
$('.click').on('click', function() {
var isClicked = $(this).data('clicked', 'yes');
if(isClicked[0].className == 'click'){
$(orangeBarClick).insertBefore("#spl-footer");
}else{
$(orangeBar).insertBefore("#spl-footer");
}
});
</script>