gpt4 book ai didi

jquery - 如何聚焦动态创建的 "a"标签?

转载 作者:可可西里 更新时间:2023-11-01 13:36:47 25 4
gpt4 key购买 nike

我试图让“a”标签在通过 ajax 调用创建后获得焦点。

代码如下:

    <form method="post" id="submitrow">
<table>
<tr>
<td>
<input name="quantity" id="id_quantity" />
</td>
<td>$
<input name="price" id="id_price" />
</td>
<td><span id="add_row"></span>

</td>
</tr>
</table>
</form>

<script>
jQuery("#id_quantity").change(function () {
jQuery('#add_row').html('<a onclick="document.forms[\'submitrow\'].submit();" id="sbmbtn">click me</a>');

jQuery('#sbmbtn').focus(); // I want to focus the new link, it does not works

alert(jQuery('#sbmbtn').is(':focus')); // it should be True!

jQuery('#sbmbtn').live("keyup", function (e) {
if (e.keyCode == 13) {
alert('whoot whoot!');
}
});
});

jQuery("#id_quantity").focus();
</script>

在 html 替换后,DOM 似乎没有正确更新。也许我必须以不同的方式替换代码?

我创建了一个 fiddle使用我的代码的简化版本重现问题。

提前感谢您提供的任何提示! :)

最佳答案

只需在a标签上设置tabindex

jQuery("#id_quantity").change(function () {
jQuery('#add_row').html('<a tabindex="11" onclick="document.forms[\'submitrow\'].submit();" id="sbmbtn">click me</a>');

jQuery('#sbmbtn').focus(); // I want to focus the new link, it does not works

alert(jQuery('#sbmbtn').is(':focus')); // it should be True!

jQuery('#sbmbtn').live("keyup", function (e) {
if (e.keyCode == 13) {
alert('whoot whoot!');
}
});
});

jQuery("#id_quantity").focus();

关于jquery - 如何聚焦动态创建的 "a"标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649930/

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