简体   繁体   English

在 jQuery 中调用 Javascript function

[英]Calling a Javascript function in jQuery

Well it looks like how I tried to explain before wasn't working as well.好吧,看起来我之前试图解释的方式并没有奏效。 I'll just explain my full purpose, show my code and what I'm doing.我将解释我的全部目的,展示我的代码和我在做什么。 I am writing a Google Chrome extension which is mainly run with jQuery.我正在编写一个 Google Chrome 扩展程序,它主要与 jQuery 一起运行。 The purpose of the extension is to provide a few css changes, script changes, and site changes to a website I am a member of.扩展的目的是为我所在的网站提供一些 css 更改、脚本更改和站点更改。 I do not have administrative access to the site so I do not have access to change the raw files and simply add an "onLoad='create_toolbar()'" attribute to the body.我没有对该站点的管理访问权限,因此我无权更改原始文件,只需将“onLoad='create_toolbar()'”属性添加到正文。 When the body/page/document loads, I want to run a function which is in the site's Javascript called "create_toolbar".当正文/页面/文档加载时,我想运行一个 function,它位于站点的 Javascript 中,称为“create_toolbar”。 I need to find a way to call the function of create_toolbar when the page is loaded but still run the jQuery in my extension which cleans up some bugs on the site.我需要找到一种方法来在页面加载时调用 create_toolbar 的 function 但仍然在我的扩展程序中运行 jQuery 以清除站点上的一些错误。

Is there a way for me to call the create_toolbar function when the document is ready with jQuery?当文档准备好 jQuery 时,我有没有办法调用 create_toolbar function? Can I append an attribute to an empty div for when onload it calls the create_toolbar function?我可以 append 一个空 div 的属性,以便在加载时调用 create_toolbar function?

I've tried the following and none have worked.我尝试了以下方法,但都没有奏效。

$(document).ready(create_toolbar);

. .

$(document).ready(function(){
$(body).attr('onLoad', 'create_toolbar()');
};

Never mind guys, I used the following code below to solve my problem:没关系,我使用下面的代码来解决我的问题:

$(document).ready(function(){
$('body').attr('onMouseOver', 'create_toolbar()');
});

To expand on @Gabriel's answer, in the following cases the OP's original code should work as is:为了扩展@Gabriel的答案,在以下情况下,OP的原始代码应该按原样工作:

function create_toolbar() {
    alert("ran it");   
};
$(document).ready(create_toolbar);

. .

$(document).ready(create_toolbar);
function create_toolbar() {
    alert("ran it");   
};

. .

var create_toolbar = function(){
    alert("ran it");   
};
$(document).ready(create_toolbar);

In this case, the OP's code would not work, but @Gabriel's solution would:在这种情况下,OP 的代码将不起作用,但@Gabriel 的解决方案将:

//Doesn't work
$(document).ready(create_toolbar);
var create_toolbar = function(){
    alert("ran it");   
};

. .

//Works
$(document).ready(function() { create_toolbar(); });
var create_toolbar = function(){
    alert("ran it");   
};

The reason is that in the OP's code, create_toolbar needs to exist at the time the line $(document).ready(create_toolbar);原因是在OP的代码中, create_toolbar需要在$(document).ready(create_toolbar);行存在。 is executed.被执行。 In @Gabriel's solution with a closure, create_toolbar doesn't need to exist until the document is ready.在@Gabriel 的带有闭包的解决方案中, create_toolbar在文档准备好之前不需要存在。

Closures:闭包:

$(document).ready(function() { create_toolbar(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM