我在 WordPress 网站上使用“onclick”来触发事件。代码是:
<a onclick="$zoho.salesiq.floatwindow.visible('show');">
[tt_vector_box icon="fa-comments" size="fa-4x" color="#FF8300" link_to_page="" target="" description=""]
<h3>Customer Service</h3>
Do you have any questions?
Talk to our Live Help online service
[/tt_vector_box]
</a>
当尝试保存代码时,WordPress 会删除 onclick 对象,留下:
<a>
[tt_vector_box icon="fa-comments" size="fa-4x" color="#FF8300" link_to_page="" target="" description=""]
<h3>Customer Service</h3>
Do you have any questions?
Talk to our Live Help online service
[/tt_vector_box]
</a>
如何解决这个问题?
PS。如果我必须编辑或添加代码,请告诉我在哪里添加或编辑它以及它可能在哪里?谢谢
最佳答案
使用 wordprtess shortcode要在保存的帖子中实际包含 js 代码,或者如前所述,使用外部 js 文件并使用此外部文件中的代码定位元素(使用某些 ID)。
要使用短代码,请执行以下操作:
// [onlick text="foo"]
function myonlick_func( $atts ) {
$a = shortcode_atts( array(
'text' => 'click here'
), $atts );
return '<a onclick="$zoho.salesiq.floatwindow.visible(\'show\');">'.$a['text'].'</a>';
}
add_shortcode( 'onlick', 'myonlick_func' );
像这样使用(在wordpress帖子中):
[onclick text="click here"]
已编辑问题的更新:
Wordpress 解析短代码的 API 不会解析嵌套的短代码(那些相似的),但可以解析不同的嵌套短代码according to this wordpress post (这是 WordPress 中使用短代码的一个已知问题,各种插件和库使用自己的短代码解析方案来覆盖默认行为)
新问题的短代码解决方案是创建一个如下所示的上下文短代码:
// [link_onlick][/link_onlick]
function myonlick_func( $atts, $content = '' ) {
$a = shortcode_atts( array(
'text' => 'click here'
), $atts );
return '<a onclick="$zoho.salesiq.floatwindow.visible(\'show\');">'.do_shortcode( $content ).'</a>';
}
add_shortcode( 'link_onlick', 'myonlick_func' );
像这样使用:
[link_onlick]
[tt_vector_box icon="fa-comments" size="fa-4x" color="#FF8300" link_to_page="" target="" description=""]
<h3>Customer Service</h3>
Do you have any questions?
Talk to our Live Help online service
[/tt_vector_box]
[/link_onlick]
这个新的短代码是自定义的,由您创建(默认情况下,wordpress 中不存在)。新短代码 [link_onlick][/link_onlick]
的代码可以放入您的 WordPress Functions.php 文件中(该文件为您的 WordPress 站点定义自定义函数)
关于javascript - 如何在 WordPress HTML 中包含 “onclick”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28992883/