javascript - 在 HTML 中的 JavaScript 中插入 HTML

标签 javascript html css if-statement

我对编程相当陌生,尤其是 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/

相关文章:

javascript - 让 YouTube 视频在弹出叠加层后停止

javascript - Django Ajax 登录表单

css - Angular 淡入淡出过渡不起作用

html - 隐藏 html 表单放大元素

Javascript 将一个函数在另一个函数完成后排队

javascript - 对象未传递给 jQuery 对话框 OnSubmit 处理程序

javascript - 单击 angularjs 中的 div 转到特定网址

javascript - Promises 和 Deferred : . did() 和 .then() 被调用,尽管 .resolve() 在 Deferred 对象上被调用

html - Jquery Click 事件在第一次点击时不触发,但在第二次点击时触发,为什么?

javascript - 在一个 div 中居中多个 div