最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery, Select by attribute value, adding new attribute - Stack Overflow

matteradmin15PV0评论

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.

How do I handle that?

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.

How do I handle that?

Share Improve this question edited Sep 16, 2010 at 6:42 Yi Jiang 50.2k16 gold badges138 silver badges136 bronze badges asked Sep 16, 2010 at 6:36 DarkLeafyGreenDarkLeafyGreen 70.5k136 gold badges391 silver badges616 bronze badges 1
  • Show us the code please. – Q_Mlilo Commented Sep 16, 2010 at 6:44
Add a ment  | 

3 Answers 3

Reset to default 4

With HTML like:

<a href='#' page='1'>Link 1</a>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​<br />

you could do:

$('a[page=1]').addClass('selected').attr('test', 'hi!');​

(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).

To select an element by attribute value, you can use this syntax:

$('[attribName="value here"]')

Where attribName should be replaced with attribute name such as name, title, etc.

To add a new attribute value, you can use the attr method.

Example:

$('[attribName="value here"]').attr('attribute name', 'attribute value');

And this is how you can change the background color:

$('[attribName="value here"]').css('background-color', 'green');

Note you should replace dummy attribute names and values as per your requirements.

Not sure what you're asking.. do you need to find the a elemenent with a certain value for "page" and change its background?

$("a[page='value']").css('background-color', 'color-code');

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far