简体   繁体   English

在按钮上单击如何更改焦点

[英]On button click how to change focus

I have a html page with n numbers of anchor tags throught the body and a button tag 我有一个html页面,身体上有n个锚标签和一个按钮标签

<input type="button" value="Next" id="btn1">
<a id="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....  <br>
<a id="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....<br>
<a id="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....<br>
<a id="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....<br>
<a id="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....<br>

The id attribute of all anchor tags is same Now i just want that when clicking on button the focus on anchor tags should get change one by one for this i have tried this jquery code but the focus is not changing from first tag 所有锚标签的id属性都是相同的现在我只想要点击按钮时锚点标签的焦点应该逐个变化,我已经尝试过这个jquery代码,但重点不是从第一个标签改变

$("#btn1").on("click", function () {
    $(this).next("#abc1").focus();
 });

if possible the solution would need to be based using Javascript and not something like JQuery. 如果可能的话,解决方案需要使用Javascript而不是像JQuery那样。

Any help greatly appreciated 任何帮助非常感谢

ID Should be unique your html is Invalid ID应该是唯一的,你的HTML是无效的

Try class of same name instead of id of same name 尝试使用相同名称的class而不是同名的id

Use this html: 使用这个html:

<input type="button" value="Next" id="btn1">
<a class="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....  <br>
<a class="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....<br>
<a class="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....<br>
<a class="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....<br>
<a class="abc1" href=""><mark style="background:red">john</mark></a> is brilliant human.....<br>

Jquery: jQuery的:

$("#btn1").on("click", function () {
    $(this).next(".abc1").focus();
 });

DEMO DEMO

Complete Working snippet: 完整的工作片段:


var pos = 0;
$("#btn1").on("click", function () {
    $('a').closest(".abc1").eq(pos).focus();
    pos = (pos<4)?++pos:0;
 });

Updated DEMO 更新了DEMO

If you are working with id's, they should be unique so you need to use class for same names. 如果您正在使用id,它们应该是唯一的,因此您需要使用相同名称的类。 change 更改

$("#btn1").on("click", function () {
    $(this).next("#abc1").focus();
 });

to

$("#btn1").on("click", function () {
    $(this).next(".myNiceClass").focus();
 });

and give a class name to your holder html element. 并给你的持有人html元素一个类名。 I assume you are storing your texts in a tag, so it should be 我假设你是存储在您的文章a标签,所以它应该是

Change 更改

<a id="abc1">

to

<a class="myNiceClass">

That's easy 这很容易

These answers actually won't work for the end game of what you're attempting... 这些答案实际上不适用于你正在尝试的最终游戏......

Each one has the right thought process, but really you need to adjust their code to actually achieve what you're wanting. 每个人都有正确的思维过程,但实际上你需要调整他们的代码才能真正实现你想要的。

The code provided by other answers will go to the next class element FROM the button. 其他答案提供的代码将转到按钮的下一个类元素。 You want from given class (ie first class, the second class... etc). 你想从给定的类(即第一类,第二类......等)。

$('#btn1').on("click",function(){
   var cur = $(this).next(".focused");
   if(cur.length > 0){
      cur.removeClass("focused").next(".abc1").focus().addClass("focused");
   }else{
      $(this).next("abc1").focus().addClass("focused");
   }
});

Do all the other stuff to change the classes... @Manwal uses a very well structured code set. 做其他所有改变类的东西...... @Manwal使用结构很好的代码集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM