Javascript 返回带有onclick cookie值的html按钮
对于一些wordpress网站,我正在创建一个cookiewall。因此,在插件文件中,我创建了如下短代码:Javascript 返回带有onclick cookie值的html按钮,javascript,wordpress,shortcode,Javascript,Wordpress,Shortcode,对于一些wordpress网站,我正在创建一个cookiewall。因此,在插件文件中,我创建了如下短代码: function agreebutton($atts, $content = null) { extract(shortcode_atts(array($atts),$content)); return '<button type="submit" onClick="SetCookie('clickagree','yes')">'; do_shortcode($content
function agreebutton($atts, $content = null) {
extract(shortcode_atts(array($atts),$content));
return '<button type="submit" onClick="SetCookie('clickagree','yes')">';
do_shortcode($content) . '</button>';
add_shortcode('button', 'agreebutton');
功能按钮($atts,$content=null){
提取(短码_atts(数组($atts),$content));
返回“”;
不要使用短代码($content)。“”;
添加快捷码(“按钮”、“约定按钮”);
在自定义模板中,我设置了一个SetCookie函数,因此如果单击按钮,cookie将被放置:
<script>
function SetCookie(cookieName,cookieValue) {
var today = new Date();
var expire = new Date();
var nDays=365
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString(); }</script>
函数SetCookie(cookieName、cookieValue){
var today=新日期();
var expire=新日期();
变量nDays=365
expire.setTime(今天.getTime()+3600000*24*nDays);
document.cookie=cookieName+“=”+escape(cookieValue)+“expires=“+expire.TogmString();”}
现在的问题是,当我尝试返回onClick=“SetCookie('clickagree','yes')时,我得到了一个意外的语法错误'clickagree'(T\u字符串)。
转义并不能解决这个问题,为了使短代码有效,您需要返回而不是回音
有什么想法吗?好的,所以在更改报价后,这起到了作用:
return '<button onClick="SetCookie( \'clickagree\',\'yes\')">' . do_shortcode($content) . '</button>';
返回“”。不使用短代码($content)。“”;
您发布的PHP示例代码不完整,我想您的示例太短了……但看看您的代码在这里的呈现方式,您使用的引号显然存在错误。检查此问答以获得解决方案:您是对的,在更改了引号后,它确实起了作用。谢谢!