jQuery,按属性值选择,添加新属性 [英] jQuery, Select by attribute value, adding new attribute
问题描述
我在HTML中有一个锚点.它具有带有值的页面属性.因此,每次单击时,我都会在我的js中使用page属性值.现在,我想使用背景颜色设置样式属性,以显示已选择某个元素.因此,我必须按页面属性选择元素,并向a元素添加一个具有值的新属性.
I have an anchor in my HTML. it has a page attribute with a value. So everytime it is clicked I use the page attribute value in my js. Now I want to set a style attribute with a background color to show that a certain a element is selected. So I have to select the element by page attribute and add a new attribute with a value to the a element.
我该如何处理?
推荐答案
使用HTML之类的
<a href='#' page='1'>Link 1</a><br />
您可以这样做:
$('a[page=1]').addClass('selected').attr('test', 'hi!');
(即,比使用style属性更好地使用css类[例如,'selected')更改显示-但那里的attr调用显示了如何添加属性).
(i.e. better to change display using a css class [e.g. 'selected'] than the style attribute - but the attr call there shows how to add an attribute).
这篇关于jQuery,按属性值选择,添加新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!