简体   繁体   中英

Adding an image after certain href with jQuery

I'm trying to add a pdf.png to the beginning of every link that ends with .pdf with the following code:

$("a[href$=.pdf]").prepend('img src=/PATH/TO/THE/IMAGE/pdf.png');

but it's not doing the trick.

When I debug it with:

$("a[href$=.pdf]").click(function(){
    alert("I am an alert box");
    $(this).hide();
    });
});

it doesn't event pop the alert box out. I've taken a look at other questions in stackoverflow and it should work but aparently it doesn't. Could anyone help me on this?

You are missing out the single quotes . Please read more about it over here. endswith selector . And also you need to wrap the tag name inside a pair of angular brackets (ex: <div/>) while your are creating a new element , Your code does not contains that part too.

Try this,

$("a[href$='.pdf']").prepend('<img src="/PATH/TO/THE/IMAGE/pdf.png"/>');

DEMO

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