简体   繁体   中英

Can html5 'a' attributes be added using jQuery

I have this code (Chrome and Firefox only)

HTML:

<a id="savefile"">
    Save transferred file
</a>

javascript:

// event is returned by a readAsDataURL()
var save = document.getElementById('savefile');
save.href = event.target.result;
save.target = '_blank';
save.download = fileName;

Besides the first line, is line 2, 3 and 4 possible in jQuery? I understand this is a non-space saving question - however It's in the middle of a jQuery subject tutorial I'm trying to compose on dataBlobs and browser compatibility.

I've been looking for about an hour - have had no luck finding the info, 'download' being the worlds worst keyword.

You can set all those attributes using one call to attr() :

$("#savefile").attr(
    {
        "href" : event.target.result,
        "target" : "_blank",
        "download" : filename
    }
);

Official documentation: http://api.jquery.com/attr/

var elem = $("#savefile")
elem.attr( "attribute-name" , "attribute-value" )

Using jQuery you can add attributes to any element you can select.

See documentation .attr()

Example usage

$("#savefile").attr("target", "_blank");

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