【发布时间】:2020-02-22 18:42:08
【问题描述】:
我正在尝试创建一个 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'")
})
【问题讨论】:
-
这里是我尝试添加 div 的地方: $(".button").on("click", function() { $(promptMsg).text("什么样的字符会您想在密码中包含吗?"); $(cntrContent).innerHTML("MULTIFACETED DIV HERE"); 有没有更直接的方法来解决这个问题?
-
你能给我们看一些代码吗?
-
如果不发布您的一些代码和您尝试过的内容,您将不会得到有意义的答案。
-
这里是 html:@user72503948573038 使用代码(格式化)更新您的原始帖子。还有你想达到什么目的?
标签: javascript html function dom innerhtml