简体   繁体   中英

I want to add class to anchor tag which is in td using jquery

This is my td:

<td width="64%" valign="top" class="addtocart_vak">
<a href="/ShoppingCart.asp?ProductCode=QH1225264">
<img border="0" align="absmiddle" src="btn_addtocart_small.gif">
</a>
</td>

I tried next jquery code:

$('.addtocart_vak' 'a').addClass("anchor");

I also tried next:

$(".addtocart_vak a[href$=/ShoppingCart.asp]").addClass("anchor");

Please give me correct solution.

Update:

I get the following JavaScript Exception:

[Exception... "'JavaScript component does not have a method named: "onStatusChange"' when calling method: [nsIWebProgressListener::onStatusChange]" nsresult: "0x80570030 (NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED)" location: "" data: no]

can you give me solution how to fix that error??

You can only provide one string as the selector, but you are breaking your string immediately after the class-selector. You probably get a syntax-error there, right?

If you don't end your selector between the class selector and the tag selector, you will be fine.

Try this instead:

$('.addtocart_vak a').addClass("anchor");

With this selector (your second attempt):

$(".addtocart_vak a[href$=/ShoppingCart.asp]")

What you say is give me all a elements having an href-attribute that ends with /ShoppingCart.asp . (You should have put quotation-marks around the value though.) However, since your href-attribute have a querystring, the href-value doesn't end with /ShoppingCart.asp . It does however being with that value, so you could have used the attribute starts with selector instead.

$(".addtocart_vak a[href^='/ShoppingCart.asp']")

Update:

The error that you'be shown has nothing to do with jQuery or your selector, but it probably breaks the execution of your script so that your jQuery code doesn't get run. Do you have any ideas on what might be causing that exception? If you fix that, your jQuery-code will probably run fine as well.

$('.addtocart_vak a').addClass("anchor");

或者,您想要该规格的直接子代

$('.addtocart_vak > a').addClass("anchor");

您可以将ID添加到锚中,然后按ID查找或直接使用

$('.addtocart_vak a').addClass("anchor");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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