访问外部JavaScript文件,并将内容DIV的最好方法? [英] Best way to access external JavaScript file and place contents in div?

查看:91
本文介绍了访问外部JavaScript文件,并将内容DIV的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,可以说你有一个网页,想从一个JavaScript文件加载,它包括

So, lets say you have a page that wants to load from a javascript file and it includes

temp.html文件

temp.html file

<script src="example.js"></script>
<p class="one"></p>

现在在example.js文件,你有一个功能,就是

Now in the example.js file you have a function that is

    function getInfo() {
    var place = "foo"
    $(".one").html(place);
    }
//Edit currently I call the function inside the JS file 

的getInfo();

我的问题是如何将您连接两个文件,​​使外部JavaScript文件都知道,它是指与类中的一个段落? 通常,当这是在一个单一页面,你会调用该函数和信息将被设置。 我见过一个getScript加入方法和jQuery的负载方法。请问这是否适用吗? 任何想法如何处理呢?如果你提供一些code,这将是超级有帮助的。 先谢谢了。

My question is how would you connect the two files so that the external javascript file knows that it is pointed to the paragraph with the class one? Normally when this is in a single page, you would call the function and the info will be set. I have seen a getScript method and a load method for Jquery. Would that be applicable here? Any ideas on how to approach this? If you provide some code that will be super helpful. Thanks in advance.

推荐答案

看起来像要执行的getInfo()一旦它的定义(即: example.js 加载)。

Looks like you want to execute getInfo() as soon as it's defined (i.e.: example.js is loaded).

您可以试试这个方法:

<script src="example.js" onload="getInfo();"></script>

在您的 example.js ,将的getInfo()来是这样的:

In your example.js, change getInfo() to something like this:

function getInfo() {
  $(document).ready(function() {
      var place = "foo"
      $(".one").html(place);
  });
}

这篇关于访问外部JavaScript文件,并将内容DIV的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆