gpt4 book ai didi

javascript - 无法同时从 awesomefont 更改文本和图标

转载 作者:行者123 更新时间:2023-11-30 12:16:09 27 4
gpt4 key购买 nike

我知道这很简单,但我不明白为什么它不起作用我只需要使用非常简单的 javascript 更改按钮的标签(里面有很棒的图标),如下所示。如果只有一件事(图标或文本),此代码也可以正常工作,但如果我尝试更改两者,则会导致错误

Uncaught TypeError: Cannot read property 'classList' of null

HTML

<body>
<h1 class="text-center">Button Test Bed</h1>
<div class="container-fluid">
<button type="button" class="btn btn-default go" id='_edit'><i class="fa fa-pencil-square-o"></i> Edit</button>
</div>
</body>

Javascript

var button = document.querySelector('#_edit');
button.addEventListener('click', function(event) {
buttonChageState(event, this);
}, false);

function buttonChageState(event, target) {
this.buttonState ;
// console.log('event:', event);
// console.log('target', target);

this.icon = target.querySelector('i');
if (this.buttonState == 0 || typeof (this.buttonState) == 'undefined') {
console.log('this.buttonState:', this.buttonState);
icon.classList.remove("fa-pencil-square-o");
icon.classList.add("fa-ban");
target.innerHTML = "Cancel";

this.buttonState = 1;
} else if(this.buttonState==1) {
console.log('this.buttonState:', this.buttonState);
icon.classList.remove("fa-ban");
icon.classList.add("fa-pencil-square-o");
target.innerHTML = "Edit";

this.buttonState = 0;
}
}

最佳答案

问题是您在本例中使用了“this”和 target.innerHTML。请改用此代码:

var icon = target.querySelector('i');

icon.innerHTML = "Cancel";

在这里摆弄: http://jsfiddle.net/kikill/2pLwyaxm/

关于javascript - 无法同时从 awesomefont 更改文本和图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32373888/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com