Native JS Implementation click to download pictures

Source: Internet
Author: User

Click to download, you can download the parent sibling node

<Divclass= "Form-group">      <Div>           <H3>Applicant Information</H3>           <Divclass= "Sq_info">                 <ul>                      <Li><span>Name:</span><P>Tom</P></Li>                      <Li><span>Name:</span><P>Tom</P></Li>                      <Li><span>I Photo:</span><imgsrc= "Http://pic.pptbz.com/201506/2015070581208537.JPG"alt=""><ahref= "javascript:void (0);"onclick= "down_img (this)">Click to download</a></Li>                      <Li><span>Passport Homepage:</span><imgsrc= "Https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=302701032,2300144492&fm=27&gp=0.jpg" alt=""><ahref= "javascript:void (0);"onclick= "down_img (this)">Click to download</a></Li>                       <Li><span>Price:</span><Pclass= "Price">500 USD</P></Li>                    </ul>               </Div>          </Div>     <Div>
<script>functionMybrowser () {varuseragent = navigator.useragent;//get the useragent string for the browservarIsopera = Useragent.indexof ("Opera") >-1;if(Isopera) {return"Opera"}; //determine if Opera browserif(Useragent.indexof ("Firefox") >-1) {return"FF";} //determine if Firefox browserif(Useragent.indexof ("Chrome") >-1) {return"Chrome";}if(Useragent.indexof ("Safari") >-1) {return"Safari";} //determine if Safari browserif(Useragent.indexof ("compatible") >-1 && useragent.indexof ("MSIE") >-1 &&!Isopera) {return"IE";}; //determine if IE browserif(Useragent.indexof ("Trident") > 1) {return"Edge";} //determine if the Edge browser}//②ie Browser picture save (ie actually use is window.open)functionSaveAs5 (imgurl) {varOPop = window.open (Imgurl, "", "width=1, Height=1, top=5000, left=5000"); for(; OPop.document.readyState! = "complete";) {if(OPop.document.readyState = = "complete") Break;} OPop.document.execCommand ("SaveAs"); Opop.close ();}//④ Click event Download (just change the picture path)functionDown_img (a) {varOSB =A;varSB =a.previoussibling.src;//console.log (SB)if(Mybrowser () = = = = "IE" | | mybrowser () = = = "Edge") {//IE (browser)SaveAs5 (SB);} Else {//! IE (non IE)Osb.href =Sb;osb.download= "";}}</script>

Note: PreviousSibling take the parent sibling node, note that there can be no spaces between the labels, otherwise there will be undefined

Native JS Implementation click to download pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.