我正在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网站的自定义函数)