如何用js实现点击按钮下载文件

JavaScript014

如何用js实现点击按钮下载文件,第1张

在按钮上写属性 onclick="函数名()" ,作用是绑定click事件

<input type="button" onclick="Click()" value="Download" />

然后在脚本处写上对应的函数即可,在函数中跳转到相应的文件路径就行。如

function Click(){

    window.location.href="文件的url地址"

}

js脚本在html中批量加入图片,我们需要设置一个父类的div,然后在创建子类,在创建好一个img的子类,将图片都添加进来,

<html>

<body>

<div id='parent'>

<div class='pin'>

</div>

</div>

</body>

<script>

var oPin = document.createElement('div')

oPin.className = 'pin'

oParent.appendChild(oPin)

var oImg = document.createElement('img')

loadImg('./image/'+ data[i].src,callBack,oImg) //这里是你的图片地址

oImg.src = './image/'+ data[i].src

oPin.appendChild(oImg)

</script>

</html>

function saveCode(obj) { 

    var winname = window.open('', '_blank', 'top=10000') 

    winname.document.open('text/html', 'replace') 

    winname.document.write(obj.value) 

    winname.document.execCommand('saveas','','test.htm') 

    winname.close() 

}