gpt4 book ai didi

javascript - 将 Javascript 内联到不显眼的 JavaScript?

转载 作者:行者123 更新时间:2023-11-30 17:52:05 29 4
gpt4 key购买 nike

我如何将这个内联 javascript 更改为 Unobtrusive JavaScript?

<input type="button" value="Text1" onclick="this.value=this.value=='Text1'?'Text2':'Text1';">

谢谢!


感谢您的回答,但它不起作用。我的代码是:

PHP

<input id=\"button1\" type=\"button\" value=\"Text1\">

js文件

document.getElementById('button1').onclick = function(){
this.value = this.value === 'Text1' ? 'Text2' : 'Text1';
}

document.getElementById('button1').addEventListener('click', function () {
this.value = (this.value === 'Text1') ? 'Text2': 'Text1';
}, false);

我试过你告诉我的一切,但没有奏效! jquery 也没有。

我是不是忘记了什么?

最佳答案

向输入添加一个 id 并执行此操作:

document.getElementById('somebutton').addEventListener('click', function () {
this.value = (this.value === 'Text1') ? 'Text2': 'Text1';
}, false);

或使用 jQuery

$('#somebutton').on('click', function () {
this.value = (this.value === 'Text1') ? 'Text2': 'Text1';
});

关于javascript - 将 Javascript 内联到不显眼的 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18780672/

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