尝试将此jquery代码重写为javascript(我做错了吗?)

时间:2020-09-21 16:03:46

标签: javascript jquery

我有以下jquery代码(来自wordpress插件),用于链接,该链接可从下拉列表中清除所选值(并删除结果)。

我想使用javascript和NOT jquery创建完全相同的功能 我想在其他按钮上使用javascript函数,例如:

<button onClick="ClearMySelectioin ()"></button>

这是原始的jquery代码:

// Clear selection - original jquery

$(document).on( 'click' , '.ywcp_selection_clear' , function(){
    'use strict';
    var $container_single_item = $(this).closest('.ywcp_components_single_item');
    clearSelection( $container_single_item );
    var $selection_open = $container_single_item.find('.ywcp_selection_open');
    if ( $selection_open.length > 0 ) { $selection_open.trigger('click'); }
    $container_single_item.trigger('ywcp_component_selection_cleared');
    return false;
});

这是我糟糕的javascript尝试。

function ClearMySelectioin (){
        var container_single_item = document.getElementsByClassName("ywcp_components_single_item").firstChild;
        container_single_item.forEach(option => option.remove())
        var selection_open = container_single_item.getElementsByClassName('ywcp_selection_open');
        if (selection_open.length > 0) {selection_open.addEventListener("click")}
        return false;
    });

我认为自己的路线不正确。我认为我的代码有百万错误。有关将上述jquery函数重新编码为javascript函数的任何建议?

0 个答案:

没有答案