如何在WordPress HTML中包含“onclick”

How to Include “onclick” in WordPress HTML

本文关键字:包含 onclick HTML WordPress      更新时间:2023-09-26

我在触发事件的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>

我该如何解决这个问题?

附言。如果我必须编辑或添加代码,请告诉我在哪里添加或编辑它以及它可能在哪里?

谢谢

使用

wordprtess 短代码将 js 代码实际包含在保存的帖子中,或者作为 mantioned,使用外部 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 不会解析嵌套的短代码(那些相似的),但可以根据这个 wordpress 帖子解析不同的嵌套短代码(这是 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函数.php文件中(它是为您的wordpress网站定义自定义函数的文件)