Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

javascript - 在Internet Explorer中页面加载时使用JavaScript或jQuery以编程方式触发按钮单击事件(Trigger button click event programmatically using JavaScript or jQuery on page load in Internet Explorer)

I have this piece of code(我有这段代码)

window.onload = function () { $('#btnFilter').click(function (e) { btnFilter(e); }); } The function works on button click but I need that the button is clicked when the page opens.(该功能在单击按钮时起作用,但是我需要在页面打开时单击按钮。) I've tried things like $('#btnFilter').trigger( "click" );(我已经尝试过$('#btnFilter')。trigger(“ click”);) but the button still not clicked on page opening.(但按钮仍未在打开页面时单击。) How can I achieve this thing?(我怎样才能做到这一点?) I can't just call the function because I get the error "Cannot read property 'currentTarget' of undefined" beacuse I don't give any event as parameter.(我不能只调用该函数,因为我没有给出任何事件作为参数,因此收到错误消息“无法读取未定义的属性'currentTarget'”。) function btnFilter(e) { element = e.currentTarget.parentElement; //other code }   ask by NoProg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can change your 'btnFilter' to accept the button instead of the event:(您可以更改“ btnFilter”以接受按钮而不是事件:)

function btnFilter(element) { element = element.parentElement; ... } $(function() { $("#btnFilter").click(function(e) { btnFilter(this); return false; }); // Pass the button to the filter operation on load btnFilter($("#btnFilter")[0]); }); alternatively, accept the parent element directly(或者,直接接受父元素) $(function() { $("#btnFilter").click(function(e) { btnFilter(this.parentElement); return false; }); // Pass the button parent to the filter operation on load btnFilter($("#btnFilter")[0].parentElement); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...