Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加尚不存在的Onclick元素_Javascript_Html - Fatal编程技术网

Javascript 添加尚不存在的Onclick元素

Javascript 添加尚不存在的Onclick元素,javascript,html,Javascript,Html,我试图为客户找到解决方案,但我不会退出。 问题是我不能编辑HTML,所以我尝试使用Javascript <!-- This section must be hide in CSS or something --> <div class="coupon-container"> <input class="input-text" type="text" id="coupon_co

我试图为客户找到解决方案,但我不会退出。 问题是我不能编辑HTML,所以我尝试使用Javascript


<!-- This section must be hide in CSS or something --> 
                    <div class="coupon-container">
                        <input class="input-text" type="text" id="coupon_code" name="coupon_code" value="">
                                                <div class="btn-apply">
                            <button type="button" title="Toepassen" class="button-coupon medium" onclick="discountForm.submit(false)" value="Toepassen"><span><span>Toepassen</span></span></button>
                        </div>
                    </div><!-- end of section Hiding CSS --> 
                </div>
这是 HTML代码,我想添加一个

onclick=new_函数

旧代码

新代码 你可以这样做

document.getElementsByClassName("coupon-container")[0].setAttribute("onclick", "new_function();");

document.querySelector'.优惠券容器'.addEventListener'click',new_函数;这回答了你的问题吗?