我向WordPress添加了一个新的meta_box。
我有一个按钮, 按下该按钮会将字符串复制到计算机的内存中。
问题在于代码可能是形式形式的, 因为我看到以下内容:
<
form id = 'adv-settings' method = 'post'>
发生的事情是, 当你单击按钮时, 它将发送表单并刷新页面。我想取消此选项, 它将不会发送。
这是我添加到主题内的functions.php文件中的代码:
add_action( 'add_meta_boxes', 'add_meta_boxes' );
function add_meta_boxes()
{
add_meta_box(
'woocommerce-order-my-custom', __( 'COPY' ), 'order_my_custom', 'shop_order', 'side', 'default'
);
}
function order_my_custom()
{
?>
<
/form>
<
button onclick="copyToClipboard()">
Copy to clipboard<
/button>
<
button onclick="copyToClipboard('I want to get copied into my clipboard')">
Copy to clipboard<
/button>
<
script>
function copyToClipboard() {
let textToCopy= "I want to get copied into my clipboard", urlInput = document.createElement( "input" );
document.body.appendChild( urlInput );
urlInput.setAttribute( "value", textToCopy );
if ( navigator.userAgent.match( /ipad|ipod|iphone/i ) ) {
let contentEditable= urlInput.contentEditable, readOnly= urlInput.readOnly, range= document.createRange(), windowSelection= window.getSelection();
urlInput.contentEditable = !0;
urlInput.readOnly= !1;
range.selectNodeContents( urlInput );
windowSelection.removeAllRanges();
windowSelection.addRange( range );
urlInput.setSelectionRange( 0, 999999 );
urlInput.contentEditable = contentEditable;
urlInput.readOnly= readOnly
} else urlInput.select();
document.execCommand( "copy" );
document.body.removeChild( urlInput );
alert( "Successfully copied to clipboard" );
}
<
/script>
<
?php
}
谁能帮我 ?谢谢
#1【将meta_box添加到WordPress。取消表单提交】试试这个代码。添加type =’ button’ 属性。
function order_my_custom(){
?>
<
/form>
<
button type="button" onclick="copyToClipboard()">
Copy to clipboard<
/button>
<
button type="button"onclick="copyToClipboard('I want to get copied into my clipboard')">
Copy to clipboard<
/button>
<
script>
function copyToClipboard() {
let textToCopy= "I want to get copied into my clipboard", urlInput = document.createElement( "input" );
document.body.appendChild( urlInput );
urlInput.setAttribute( "value", textToCopy );
if ( navigator.userAgent.match( /ipad|ipod|iphone/i ) ) {
let contentEditable= urlInput.contentEditable, readOnly= urlInput.readOnly, range= document.createRange(), windowSelection= window.getSelection();
urlInput.contentEditable = !0;
urlInput.readOnly= !1;
range.selectNodeContents( urlInput );
windowSelection.removeAllRanges();
windowSelection.addRange( range );
urlInput.setSelectionRange( 0, 999999 );
urlInput.contentEditable = contentEditable;
urlInput.readOnly= readOnly
} else urlInput.select();
document.execCommand( "copy" );
document.body.removeChild( urlInput );
alert( "Successfully copied to clipboard" );
}
<
/script>
<
?php
}
推荐阅读
- 如何编辑此代码,使其仅影响wordpress网站上的顶部菜单,而不影响主菜单()
- 如何编辑此代码,以便仅在用户登录时才更改主页上的”顶部菜单”栏,而不更改”主菜单”()
- 用Timber添加多级菜单
- 如何在WordPress内置the_title()中显示图像()
- 对于WP上已登录和已注销的用户,如何在标题菜单上使用不同的选项()
- 如何在Bootstrap中创建可滚动的列()
- 如何更改按钮的默认WordPress登录页面链接()
- 如何将导航标题菜单栏的背景颜色从一种颜色更改为两种颜色
- 将活动类添加到wordpress循环的第一项