使用 javascript 函数将 Complex HTML 附加到 div?
Appending Complex HTML to div with javascript function?
我正在尝试创建一个 javascript 函数,它将复杂的 html 附加到已经存在的 div 中(“复杂”意味着不止一个元素,例如
或等等)。我不能将整个 div 放在 .innerHTML(“div”) 的 () 中,因为所有导致 JS 停止读取的符号都会将其注册为错误作为字符串的内容。有什么建议吗?有没有办法在我的 HTML 文件中创建元素并隐藏它,直到它被 javascript 中的函数触发?谢谢你! (html 和下面的 js)
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href= "style.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class = "row r1">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<h1>Your Password Generator</h1>
</div>
<div class = "col-md-3"></div>
</div>
<br><br><br>
<div class = "row r2">
<div class = "col-md-3"></div>
<div class = "col-md-6 hello">
<p>Hello, friend. Welcome to your new password generator. <br> Click "continue" to customize your password.</p>
</div>
<div class = "col-md-3"></div>
</div>
<br> <br>
<div class = "row r3">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<div class = "cntrContent"></div>
</div>
<div class = "col-md-3"></div>
</div>
<div class = "characterSelect">
</div><div class = "text-center">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Special Characters
</label> <br>
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Numbers
</label> <br>
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Letters
</label>
</div>
<br> <br>
<div class = "row r4">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<div class= "text-center">
<button type="button" class="btn btn-light button">Continue</button>
</div>
</div>
<div class = "col-md-3"></div>
</div>
<!-- initial display: "Hello, friend. Welcome to your new password generator. Click "continue" to customize your password
then: new display, the question: "which kinds of characters would you like to include in your password?" followed by
list with checkboxes for "special character" "numbers" and "letters". this is followed by a "generate my password" button.
their selections will run through conditionals with event listeners. the next page will display the password under header "your password"
then a button below, "copy password to clipboard" and another button that says "i want another password" which brings
user back to customization options.-->
<script src="./script.js"></script>
JS:
var promptMsg = document.querySelector(".hello");
var cntrContent = document.querySelector(".cntrContent");
var button = document.querySelector(".button");
var characterSelect = document.querySelector(".characterSelect")
$(".button").on("click", function() {
$(promptMsg).text("What kinds of characters would you like to include in your password?");
$(characterSelect).innerHTML("THIS IS WHERE I AM TRYING TO APPEND THE DIV CLASSNAME 'characterSelect'")
})
一种方法是从一开始就将您的 HTML 代码插入目标 div 中,但隐藏(使用 CSS 的 display:none
或 jQuery的hide()
).
然后,在您的函数调用中,随时使用 jQuery 的 show()
显示隐藏的 HTML,如下所示:
$(document).ready(function() {
$("#to_hide").hide();
});
//simulating some waiting before function call
//JUST FOR ILLUSTRATION PURPOSES, not needed in actual code
setTimeout(function() {
callback();
}, 3000);
//your function that will show the div and do whatever you want to do here
function callback() {
//whatever you want to do here
$(".to_hide").show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<div class="to_hide">
<p>Some content</p>
</div>
</div>
您正在将 jquery 方法与 JS 方法混合。
对于你的情况,我建议使用 template
<template id="myTemplate">
htmlto append
</template>
然后你可以使用append
.
与jquery:
var template = $("#myTemplate").html();
$(characterSelect).append(template);
与普通javascript:
var template = document.getElementById("myTemplate").content;
characterSelect.appendChild(template);
我正在尝试创建一个 javascript 函数,它将复杂的 html 附加到已经存在的 div 中(“复杂”意味着不止一个元素,例如
或等等)。我不能将整个 div 放在 .innerHTML(“div”) 的 () 中,因为所有导致 JS 停止读取的符号都会将其注册为错误作为字符串的内容。有什么建议吗?有没有办法在我的 HTML 文件中创建元素并隐藏它,直到它被 javascript 中的函数触发?谢谢你! (html 和下面的 js)
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href= "style.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class = "row r1">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<h1>Your Password Generator</h1>
</div>
<div class = "col-md-3"></div>
</div>
<br><br><br>
<div class = "row r2">
<div class = "col-md-3"></div>
<div class = "col-md-6 hello">
<p>Hello, friend. Welcome to your new password generator. <br> Click "continue" to customize your password.</p>
</div>
<div class = "col-md-3"></div>
</div>
<br> <br>
<div class = "row r3">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<div class = "cntrContent"></div>
</div>
<div class = "col-md-3"></div>
</div>
<div class = "characterSelect">
</div><div class = "text-center">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Special Characters
</label> <br>
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Numbers
</label> <br>
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Letters
</label>
</div>
<br> <br>
<div class = "row r4">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<div class= "text-center">
<button type="button" class="btn btn-light button">Continue</button>
</div>
</div>
<div class = "col-md-3"></div>
</div>
<!-- initial display: "Hello, friend. Welcome to your new password generator. Click "continue" to customize your password
then: new display, the question: "which kinds of characters would you like to include in your password?" followed by
list with checkboxes for "special character" "numbers" and "letters". this is followed by a "generate my password" button.
their selections will run through conditionals with event listeners. the next page will display the password under header "your password"
then a button below, "copy password to clipboard" and another button that says "i want another password" which brings
user back to customization options.-->
<script src="./script.js"></script>
JS:
var promptMsg = document.querySelector(".hello");
var cntrContent = document.querySelector(".cntrContent");
var button = document.querySelector(".button");
var characterSelect = document.querySelector(".characterSelect")
$(".button").on("click", function() {
$(promptMsg).text("What kinds of characters would you like to include in your password?");
$(characterSelect).innerHTML("THIS IS WHERE I AM TRYING TO APPEND THE DIV CLASSNAME 'characterSelect'")
})
一种方法是从一开始就将您的 HTML 代码插入目标 div 中,但隐藏(使用 CSS 的 display:none
或 jQuery的hide()
).
然后,在您的函数调用中,随时使用 jQuery 的 show()
显示隐藏的 HTML,如下所示:
$(document).ready(function() {
$("#to_hide").hide();
});
//simulating some waiting before function call
//JUST FOR ILLUSTRATION PURPOSES, not needed in actual code
setTimeout(function() {
callback();
}, 3000);
//your function that will show the div and do whatever you want to do here
function callback() {
//whatever you want to do here
$(".to_hide").show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<div class="to_hide">
<p>Some content</p>
</div>
</div>
您正在将 jquery 方法与 JS 方法混合。
对于你的情况,我建议使用 template
<template id="myTemplate">
htmlto append
</template>
然后你可以使用append
.
与jquery:
var template = $("#myTemplate").html();
$(characterSelect).append(template);
与普通javascript:
var template = document.getElementById("myTemplate").content;
characterSelect.appendChild(template);