简体   繁体   English

单击li元素后如何切换ul的ID?

[英]How to switch id of ul after click on li element?

I want to dynamically change id of ul element after click on li element. 我想在单击li元素后动态更改ul元素的ID。 Then I pass this id to str variable as a string. 然后,我将此ID作为字符串传递给str变量。 For an example when I click on Driver 2 id of ul changes for "?driver_id=2", 例如,当我单击驱动程序2的ul的id更改为“?driver_id = 2”时,

I've got something like this: 我有这样的事情:

<ul class="drivers" id="">
     <li><a href="" id="?driver_id=1">Driver 1</a></li>
     <li><a href="" id="?driver_id=2">Driver 2</a></li>
     <li><a href="" id="?driver_id=3">Driver 3</a></li>
 </ul>

and jquery: 和jQuery的:

var str=$('ul.drivers').attr('id');

as some of the comments mentioned, this is not a good idea. 正如一些评论所提到的,这不是一个好主意。 The id attribute is supposed to be unique, so by setting the id of the ul to the id of one of the li s, youre creating two ids on your page, which will mask one of them if you try searching for it again. id属性应该是唯一的,因此通过将ul的id设置为li之一的id,可以在页面上创建两个id,如果您再次尝试搜索它们,它们将掩盖其中的一个。

but if you're trying to change attributes with jquery, use the second argument of attr to set the attrib 但是,如果您尝试使用jquery更改属性,请使用attr的第二个参数来设置attrib

$('ul.drivers').attr('id', value)

if you want to use class instead of id, use jquerys addClass method. 如果要使用类而不是id,请使用jquery的addClass方法。

$('ul.drivers').addClass(value)

though i would recommend using a custom data attribute 虽然我建议使用自定义数据属性

$('ul.drivers').data('some-name', value) //=> set value
$('ul.drivers').data('some-name') //=> get value

Using the id attribute isn't a great idea because they must be unique, and shouldn't be changing throughout the DOM. 使用id属性并不是一个好主意,因为它们必须是唯一的,并且在整个DOM中都不应更改。

Instead, the data attribute seems like a better use. 相反, data属性似乎是更好的用法。 You can set the attribute, and access its value using something like this: 您可以设置属性,并使用以下方式访问其值:

HTML HTML

<ul class="drivers" id="list">
   <li><a href="#" data-driver="?driver_id=1">Driver 1</a></li>
   <li><a href="#" data-driver="?driver_id=2">Driver 2</a></li>
   <li><a href="#" data-driver="?driver_id=3">Driver 3</a></li>
</ul>

JavaScript JavaScript的

$(document).ready(function() {
  $('#list li a').on('click',function() {
    var str = $(this).data('driver');
    console.log(str);
  })
})

Here it is in a JS Fiddle example: https://jsfiddle.net/igor_9000/wh3v1wsk/ 这是JS Fiddle示例: https//jsfiddle.net/igor_9000/wh3v1wsk/

Hope that helps! 希望有帮助!

Instead of changing the id, create a data attribute, like this: 无需更改id,而是创建一个data属性,如下所示:

<ul class="drivers" id="driverlist-1" data-active-driver-id="">
  <li><a href="" id="?driver_id=1">Driver 1</a></li>
  <li><a href="" id="?driver_id=2">Driver 2</a></li>
  <li><a href="" id="?driver_id=3">Driver 3</a></li>
</ul>

<ul class="drivers" id="driverlist-2" data-active-driver-id="">
  <li><a href="" id="?driver_id=4>Driver 4</a></li>
  <li><a href="" id="?driver_id=5">Driver 5</a></li>
  <li><a href="" id="?driver_id=6">Driver 6</a></li>
</ul>

Data Attributes are meant to store data you need to control for any reason. 数据属性用于存储由于任何原因需要控制的数据 Please, refer to this link . 请参考此链接

Then you change it like this, accessing your ul with its id, because think, if you had two uls each one with its own drivers , you'd guarantee the proper one would be changed: 然后您可以像这样更改它,并使用其id访问您的ul,因为如果您有两个ul,每个ul都有自己的驱动程序 ,则可以保证将更改正确的ul:

$('#driverlist-1').data('active-driver-id', value); // setting some value to it
var str = $('#driverlist-1').data('active-driver-id'); // getting stored value

where value is the id of the clicked li . 其中value是单击的li的id。 I'd recommend following this even if you don't have two lists at all, just to keep good practices. 即使您根本没有两个列表,我还是建议您遵循此规则,只是为了保持良好的操作习惯。

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

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