我正在尝试从选择框下拉菜单中删除一个项目(value=155 / 'Fruits')。
<div class="control select">
<select id="product_cat"
name="product_cat[]"
class="category-select2 select2-hidden-accessible"
style=""
data-parsley-maxcheck="3"
multiple=""
data-parsley-multiple="product_cat[]"
tabindex="-1"
aria-hidden="true"
>
<option class="level-0" value="115">Misc</option>
<option class="level-0" value="161">Vegetables</option>
<option class="level-0" value="160">Grains</option>
<option class="level-0" value="155">Fruits</option>
<option class="level-1" value="156"> Apples</option>
<option class="level-1" value="157">  Bananas</option>我正在使用Wordpress,经过多次尝试,我还没能将其从下拉列表中删除。例如,我在我的function.php文件中尝试了以下JS代码:
function customcats_hook_javascript() {
?>
<script>
var select = document.getElementById('product_cat')
select.removeChild(select.querySelector('option[value="155"]'))
</script>
<?php
}
add_action('wp_head', 'customcats_hook_javascript');发布于 2021-02-11 11:44:07
在所有正文元素可用之前,在中执行的脚本可能存在问题。通常,您需要等待HTML处理完毕并完成DOM。一种可能是将脚本移动到页面的末尾,即页脚中的某个位置。理想情况下,需要等待页面加载,加载了jQuery后,您可以使用
jQuery('document').ready(function(){
// your code here
});https://stackoverflow.com/questions/66148371
复制相似问题