简体   繁体   English

包含的javascript文件中的jquery

[英]jquery in included javascript file

I am new to jquery and javascript so I haven't been able to figure this problem out; 我是jquery和javascript的新手,所以我无法弄清楚这个问题。 nor have my google-fu skills helping me today. 我的Google Fu技能今天也没有帮助我。

I developed a page with HTML and Javascript/Jquery, placing all the jquery between script tags and all was well. 我用HTML和Javascript / Jquery开发了一个页面,将所有jquery放在脚本标签之间,一切都很好。 When I was done the page I realized that I wanted the same javascript on another page so I put all that jquery/javascript into an external file and tried to include it. 当我完成该页面时,我意识到我希望在另一个页面上使用相同的javascript,因此我将所有jquery / javascript都放入了一个外部文件,并尝试包含它。

That's where things went downhill. 那就是事情的发展。 The javascript doesn't work at all on the initial page. javascript在初始页面上根本不起作用。 Nothing happens. 什么都没发生。 I checked the error console and I am not getting any JS errors. 我检查了错误控制台,但没有收到任何JS错误。

Why does this not work and how do I get it to work? 为什么这不起作用?如何使它起作用?

This is the JS code in the external file: http://pastebin.com/wHBmbHLs 这是外部文件中的JS代码: http : //pastebin.com/wHBmbHLs

The problem probably has little to do with your Javascript since you're not even getting any errors. 这个问题可能与您的Javascript无关,因为您什至没有遇到任何错误。 It likely has everything to do with the manner in which you're including the script. 它可能与您包含脚本的方式有关。 Does your script tag look like this: 您的script标签是否如下所示:

<script type="text/javascript" src="your source"></script>

Or perhaps more to the point...we need to see your html in order to begin to help you here. 或许还有更多要点...我们需要查看您的html才能在这里开始为您提供帮助。

For reference, in the future if you have a problem like this, try to isolate the problem. 作为参考,将来如果您遇到这样的问题,请尝试找出问题所在。 For example, create a small Javascript file that contains one line: 例如,创建一个包含一行的小Javascript文件:

alert('hello');

Then include it. 然后包括它。 If it fails to run, you know the error is in your HTML, and not in your Javascript. 如果无法运行,则说明错误出在HTML中,而不是Javascript中。

Difficult to tell without seeing the HTML, but it is probably that you simply have the wrong script tag (as Surreal points out for script to work in IE is has to be type "text/javascript", even though this is a depreciated option. The newer "application/javascript" won't work in IE7 or IE8 as far as I know) 很难看清而不用看HTML,但是很可能您只是使用了错误的脚本标签(如Surreal指出,要在IE中运行脚本,必须键入“ text / javascript”,即使此选项已弃用)。据我所知,较新的“应用程序/ javascript”无法在IE7或IE8中运行)

You might also want to check the Javascript itself, you have a few funny things going on. 您可能还需要检查Javascript本身,因此您可能会遇到一些有趣的事情。 For example you have a function name "postBox" in which you define a variable "postBox". 例如,您有一个函数名称“ postBox”,其中定义了一个变量“ postBox”。 Don't know if this is legal Javascript but even if it is that doesn't mean an interpreter won't get confused. 不知道这是否是合法的Javascript,但是即使是那也不意味着解释器不会感到困惑。

Also use jQuery to make dynamic tags. 还可以使用jQuery制作动态标签。 Use something like 使用类似

var newDiv = jQuery(""); var newDiv = jQuery(“”); var newInput = jQuery(""); var newInput = jQuery(“”); newInput.attr("type", "text"); newInput.attr(“ type”,“ text”); newInput.css({"width": "500px"}; newDiv.append(newInput); newInput.css({“ width”:“ 500px”}; newDiv.append(newInput);

Takes up more space but this is less error prone and can give better errors when it goes wrong. 占用更多空间,但是这样更不容易出错,并且在出错时可以提供更好的错误。 Browsers like IE8 will fail silently if you try to insert invalid HTML from a string using jQuery, this can be very frustrating as you have no idea why it didn't work. 如果您尝试使用jQuery从字符串中插入无效的HTML,则类似IE8的浏览器将无提示地失败,这可能会非常令人沮丧,因为您不知道为什么它不起作用。

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

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