我用Javascript設計了一個複製功能,button ,同時裡面有個Form。
如果按了 button,就會執行 Form 的 summit,我不要他執行,怎麼辦?
查了網路,發現:將該 button 這樣設就好了:type="button"
<button type="button">複製</button>
<input value="要複製的內容123456" style="display:none;"/>
<button class="copy_coupon" type="button">點擊複製</button>
</div>
<script>
window.Clipboard = (function(window, document, navigator) {
var textArea,
copy;
function isOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
function createTextArea(text) {
textArea = document.createElement('textArea');
textArea.value = text;
document.body.appendChild(textArea);
}
function selectText() {
var range,
selection;
if (isOS()) {
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}
}
function copyToClipboard() {
document.execCommand("Copy");
document.body.removeChild(textArea);
}
copy = function(text) {
createTextArea(text);
selectText();
copyToClipboard();
};
return {
copy: copy
};
})(window, document, navigator);
$(".copy_coupon").on("click", function() {
var $this = $(this),
value = $this.prev("input").val();
window.Clipboard.copy(value);
});
</script>