gpt4 book ai didi

javascript - 如何在文档准备好时触发单击事件...?

转载 作者:行者123 更新时间:2023-12-01 02:00:06 25 4
gpt4 key购买 nike

单选按钮

<input name="radio-choice-t-6" class="qololbl" id="radio-choice-t-6a" value="qolo" checked="checked" type="radio">
<label for="radio-choice-t-6a" style="border: 1px solid #2d5f7c;width:90px;border-bottom-left-radius:4px;border-top-left-radius:4px;font-family:Noto Sans;">Toast</label>

$(document).ready(function (e) {

$('.qololbl').trigger('click');

$(".qololbl").click(function(){
alert("Hi");
});
});

如何在文档就绪时触发 qololbl 点击事件。这段代码不起作用,为什么..?提前致谢。帮助我

最佳答案

使用.click()而不是.trigger()。并将其放在事件处理程序声明之后。

$(document).ready(function (e) {

$(".qololbl").click(function(){
alert("Hi");
});

$('.qololbl').click();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="radio-choice-t-6" class="qololbl" id="radio-choice-t-6a" value="qolo" checked="checked" type="radio">
<label for="radio-choice-t-6a" style="border: 1px solid #2d5f7c;width:90px;border-bottom-left-radius:4px;border-top-left-radius:4px;font-family:Noto Sans;">Toast</label>

替代方案(由 pratik-gaikwad 建议)

$(document).ready(function (e) {

$(".qololbl").on('click', function(){
alert("Hi");
});

$('.qololbl').trigger('click');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="radio-choice-t-6" class="qololbl" id="radio-choice-t-6a" value="qolo" checked="checked" type="radio">
<label for="radio-choice-t-6a" style="border: 1px solid #2d5f7c;width:90px;border-bottom-left-radius:4px;border-top-left-radius:4px;font-family:Noto Sans;">Toast</label>

关于javascript - 如何在文档准备好时触发单击事件...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43919570/

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