Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 如何在HTML中调用外部JavaScript函数(How to call external JavaScript function in HTML)

I have a small chunk of code I can't seem to get working.

(我有一小部分代码似乎无法正常工作。)

I am building a website and using JavaScript for the first time.

(我正在建立一个网站并首次使用JavaScript。)

I have my JavaScript code in an external file 'Marq_Msg.js' which looks like this:

(我在外部文件'Marq_Msg.js'中有我的JavaScript代码,如下所示:)

var Messages = new Array();
Messages[0] = "This is message 1";
Messages[1] = "This is message 2";
Messages[2] = "This is message 3";
Messages[3] = "This is message 4";  

function scroll_messages()
{
  for (var i = 0; i < Messages.length; i++)
        document.write(Message[i]); 
}

and in my HTML file 'Index.html' I am trying to call it like this:

(在我的HTML文件'Index.html'中我试图这样称呼它:)

    <div id="logo">
        <marquee scrollamount="5" direction="left" loop="true" height="100%" width="100%">
        <strong><font color="white"><script src="Marq_Msg.js">scroll_messages()</script></font></strong>
        </marquee>
    </div>

The 'logo' div is a CSS piece that I'm trying to marquee inside of.

('logo'div是一个我试图在其中进行选择的CSS片段。)

If I put the code embedded inside the 'head' tag and call it, it works perfectly!

(如果我把代码嵌入'head'标签内并调用它,它就能完美运行!)

There are a few other things id like to do with this code (like space the messages out a little) but I can't get the code to work in the first place.

(还有一些与此代码有关的其他事情(比如将消息空间分开一点)但我无法让代码首先工作。)

I've also tried adding:

(我也尝试过添加:)

<script src="Marq_Msg.js"></script>

in the 'head' tag with a separate call, that was a no go.

(在'head'标签中单独调用,这是不行的。)

I also tried instead using:

(我也尝试使用:)

<script type="text/javascript" src="Marq_Msg.js">scroll_messages()</script>

Hell, i even had the function try returning a string (even hardcoded a simple "hello" to be returned) but that didnt work either with and without the 'type':

(地狱,我甚至有函数尝试返回一个字符串(甚至硬编码一个简单的“你好”要返回)但是,无论有没有'类型':)

//Marq_Msg.js
function scroll_messages()
{
   return "hello";
}

//index.html
<script type="text/javascript" src="Marq_Msg.js">document.write(scroll_messages())</script>

What am I missing?

(我错过了什么?)

Any help would be greatly appreciated!!

(任何帮助将不胜感激!!)

I've looked all over Google, and every site I find wants to do it using some 'form'.

(我看了整整一遍谷歌,我找到的每个网站都想用一些'形式'来做。)

I just want messages to be displayed across, no form attached.

(我只是想要显示消息,没有附加表单。)

  ask by Kevin Fauver translate from so

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

1 Answer

0 votes
by (71.8m points)

If a <script> has a src then the text content of the element will be not be executed as JS (although it will appear in the DOM).

(如果<script>有一个src那么该元素的文本内容将不会作为JS执行 (尽管它将出现在DOM中)。)

You need to use multiple script elements.

(您需要使用多个脚本元素。)

  1. a <script> to load the external script

    (<script>加载外部脚本)

  2. a <script> to hold your inline code (with the call to the function in the external script)

    (一个<script>来保存你的内联代码(在外部脚本中调用该函数))

    scroll_messages();

    (scroll_messages();)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...