我对编程相当陌生,尤其是 HTML/CSS 和 JavaScript,所以如果我的解释不是那么好,我很抱歉。
我使用 ionic 只是为了设置模板。我的问题是,该文档是一个 .html 文件,我在 <script>
中添加了所以我可以在其中插入 Javascript。具体来说,我试图在其中添加一个 if-else 语句。但是,我还需要在 if 语句中添加 html 代码——我需要添加 {{question.a1right}}
它从 fieldbook 电子表格(这里是链接:https://api.fieldbook.com/v1/56be4f73bf3e5b030029d62a/quiz_app_q_a/1)调用,以便我可以查看执行特定操作的值是“Y”还是“N”。
如何在 HTML 文档内的 JavaScript 中插入 HTML?谢谢,请简单地解释一下概念,以便我能理解:)
<html>
<body>
<ion-view>
<ion-nav-title>Text</ion-nav-title>
<ion-content overflow-scroll="true" padding="true" class="has-header">
<div>
<button class="button button-full button-energized">
<script type="text/javascript">
<script type="text/html">
</script>
</script>
</button>
</div>
</ion-content>
</ion-view>
</body>
</html>
最佳答案
作为初学者,我首先建议你use jQuery rather than pure javascript .一方面,它的打字次数要少得多,而且(恕我直言)更容易掌握。您使用 jQuery 编写代码,然后 jQuery 在运行时在幕后将其转换为 javascript。要使用 jQuery,您所要做的就是将 jQuery 库包含在 <head>
中。每个页面的标签,像这样:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
允许您将 HTML 注入(inject)页面的 Javascript (jQuery) 如下所示:
$('#myButt').click(function(){
$('body').append('<div id="injectedDIV">This added in</div>');
});
#injectedDIV{width:100%;padding:20px;background:wheat;color:orange;text-align:center;font-size:2rem;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<ion-view>
<ion-nav-title>Text</ion-nav-title>
<ion-content overflow-scroll="true" padding="true" class="has-header">
<div>
<button id="myButt" class="button button-full button-energized">Click Me</button>
</div>
</ion-content>
</ion-view>
完整的示例页面如下所示:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#myButt').click(function(){
$('body').append('<div id="injectedDIV">This added in</div>');
});
});
</script>
<style>
#injectedDIV{width:100%;padding:20px;background:wheat;color:orange;text-align:center;font-size:2rem;}
</style>
</head>
<html>
<body>
<ion-view>
<ion-nav-title>Text</ion-nav-title>
<ion-content overflow-scroll="true" padding="true" class="has-header">
<div>
<button id="myButt" class="button button-full button-energized">Click Me</button>
</div>
</ion-content>
</ion-view>
</body>
</html>
由于您是 jQuery 新手,here are some free beginner-level tutorials让你开始。 (这是我自学的地方,而且他们是免费的)
关于javascript - 在 HTML 中的 JavaScript 中插入 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36094357/