attributes not found by jquery attribute selector

后端 未结 2 1407
眼角桃花
眼角桃花 2021-01-12 19:00

I need to reask my old question, I probably shouldnt have asked it at 1am :P

It seems that some attributes are not being found using jquery\'s attribute selector:

2条回答
  •  灰色年华
    2021-01-12 19:21

    It all depends on which jQuery version you're using.

    Before 1.3, you could use @ notation:

    $("*[@name=value]")
    

    So maybe adding @ helps.

    Other than that, you should enter the attribute value exactly the same as it's defined in the markup - e.g. if you're trying to find an image with src="http://example.com/dog.jpg", don't do this because it won't work:

    $("img[src=dog.jpg")
    

    as it will be trying to find images with src equal to "dog.jpg", not containing it.

    If you want to search attributes defining only parts of it, I'd suggest reading the jQuery API Selectors page. For example, to get all images whit src containing "dog.jpg", you could use:

    $("img[src*=dog.jpg]")
    

    Similarly, you can find elements whose attributes start or end with specific values / strings.

提交回复
热议问题