我目前正在創建自己的插件來 window.print 在我的 WordPress 網站上發布帖子。但似乎我無法通過 onclick 訪問該功能。如果我將 window.print 放在按鈕本身中它可以作業,但這不是它必須為我作業的方式。
// Only do this when a single post is displayed
if ( is_single() ) {
// Message you want to display after the post
$content .= '<button type="Submit" value="Print_PDF" onclick="GetPDF()"> Print PDF </button>';
}
// Return the content
return $content;
}```
But whenever i click the button i get an error that says that it does not acces this function:
``` function GetPDF() {
window.print();
}```
It is in the same file.
uj5u.com熱心網友回復:
嘗試這個:
// Only do this when a single post is displayed
if ( is_single() ) { ?>
<script>
function GetPDF(e) {
e.preventDefault();
window.print();
}
</script>
<?php
// Message you want to display after the post
$content .= '<button type="button" value="Print_PDF" onclick="GetPDF(event)"> Print
PDF </button>';
}
// Return the content
return $content;
}
uj5u.com熱心網友回復:
HTML 元素的 onclick 屬性查找 javascript 函式,它不能運行 PHP 函式。
看到這個問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/504791.html
標籤:javascript php WordPress 插件
