Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从一个javascript文件分别调用两个不同的函数_Javascript_Html - Fatal编程技术网

从一个javascript文件分别调用两个不同的函数

从一个javascript文件分别调用两个不同的函数,javascript,html,Javascript,Html,我有这样一个脚本: .classone{ // styles for the first } .classtwo{ // styles for the second } example.js: 我想在html页面的不同位置使用example.js中的ex1()和ex2(),并对它们进行不同的样式设置 那么如何在html中分别调用这些ex1()和ex2。可能吗?如果可能,怎么做 谢谢,如果您有从html引用的文件,您可以毫无问题地调用这些函数 你说的“造型”是什么意思? 这些功能之

我有这样一个脚本:

.classone{
   // styles for the first
}

.classtwo{
   // styles for the second
}
example.js:

我想在html页面的不同位置使用example.js中的ex1()ex2(),并对它们进行不同的样式设置

那么如何在html中分别调用这些ex1()ex2。可能吗?如果可能,怎么做


谢谢,

如果您有从html引用的文件,您可以毫无问题地调用这些函数

你说的“造型”是什么意思? 这些功能之间有什么区别

编辑OP希望在每个函数中输出不同的样式:

function ex1(){document.write('<p class="classone">example1</p>');}<br>
function ex2(){document.write('<p class="classtwo">example1</p>'');}

作为一种快速解释,
document.write
将页面内容替换为论文“
()
”:

这是因为代码的
文档
部分引用了页面正文中的所有内容(基本上是您看到的所有内容)

要将文本仅放在页面的特定部分,必须引用要首先放置文本的项目/元素。您可以通过多种方式实现这一点,例如按元素引用(如@metal_fan的示例)、按类、元素类型等。您还可以使用jQuery等专门用于引用元素的框架

jQuery版本的@metal\u fan解决方案:

eg. jQuery("#myId").html("hello world"); // Replaces content of #myId element with "hello world"
正如@Kenneth所暗示的(尽管有点苛刻),要使用好它,您可能需要增加对javascript和HTML的理解。我可以建议你报名参加旅游吗?它们非常全面,你可以获得分数,而且是免费的


祝你好运

我想要示例1——斜体、红色和示例2粗体、蓝色谢谢,我怎样才能从html中分别调用ex1()和ex2()?
我不想使用按钮。相反,我想在特定的位置看到ex1()中的字符串。我认为您缺乏Javascript和HTML的基本技能。请寻找一些关于HTML和Javascript的教程,并自己尝试一些东西。W3Schools是一个很好的起点。您似乎认为
document.write
可以直接在HTML中调用,并且它可以将您指定的内容写入您调用它的位置。您将考虑DOM API来进行DOM操作或尝试jQuery。如果你想在页面中添加一些文本或html,你应该像上面的注释那样查看DOM API。我发现了这个向页面添加内容的简单示例:
eg. document.write("hello world"); // Replaces everything with "hello world"
eg. jQuery("#myId").html("hello world"); // Replaces content of #myId element with "hello world"