同意JK,如:<script type="text/javascript"> function word_onclick() { var oWD = new ActiveXObject("Word.Application"); var oDC = oWD.Documents.Add("",0,1); var oRange =oDC.Range(0,1); var sel = document.body.createTextRange(); sel.moveToElementText(content); sel.select(); sel.execCommand("Copy"); oRange.Paste(); oWD.Application.Visible = true; } </script> <div id="content"> <h1 style="color:red;">hello,world!</h1> </div> <button onclick="try{word_onclick()}catch(e){alert('abort!')};">export to word</button>
----拒绝交互的话,创建对象会失败,可以catch到
function word_onclick()
{
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(content);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
}
</script>
<div id="content">
<h1 style="color:red;">hello,world!</h1>
</div>
<button onclick="try{word_onclick()}catch(e){alert('abort!')};">export to word</button>