不显示样式和类,当使用JS将div添加到我的HTML时

huangapple go评论64阅读模式
英文:

Doesn't show both style and class when added div to my HTML using JS

问题

以下是您要翻译的内容:

"Doesn't show both style and class in console when added div to my HTML using JS, also bootstrap classes don't work,

Here is the code for main_page:

<?php
require "connection.php";?>
<html>
<head>
	<link rel = "stylesheet" href = "css/bootstrap.min.css" />
	<link rel = "stylesheet" href = "css/style.css" />
</head>
<body>
<header>
	<?php require "header.php"; ?>
</header>
<main>
	<?php require "main_content.php"; ?>
</main>
<footer>
	<?php ?>
</footer>
</body>
<script src = "js/change_avatar_settings.js"></script>
<script src = "js/jQuery.js"></script>
<script src = "js/bootstrap.bundle.min.js"></script>
</html>

Here is the code for main_content.php:

<main class = "container mt-5">
<div class = "display-none" id = "change_display">
	<div id = "change_avatar" class = "container">
	</div>
</div>	
</main>

Here is the JS code called from onclick function (which is in header) (no problem from calling a function), the problem is in that both class - "armen" and style doesn't work in my html, but div with form is created without any issues.

function changeAvatar() {
	document.getElementById('change_avatar').innerHTML += '<div class = "armen" style = "width:500px;height:500px;"><form action="" method="post" enctype="multipart/form-data"><input type="file" name="image"><input type="submit" value="Upload Image" name="avatar_btn"><input type="submit" value="Delete Image" name="del_avatar_btn"></form></div>'
}
英文:

Doesn't show both style and class in console when added div to my HTML using JS, also bootstrap classes don't work,

Here is the code for main_page:

<?php
require "connection.php";?>
<html>
<head>
	<link rel = "stylesheet" href = "css/bootstrap.min.css" />
	<link rel = "stylesheet" href = "css/style.css" />
</head>
<body>
<header>
	<?php require "header.php"; ?>
</header>
<main>
	<?php require "main_content.php"; ?>
</main>
<footer>
	<?php ?>
</footer>
</body>
<script src = "js/change_avatar_settings.js"></script>
<script src = "js/jQuery.js"></script>
<script src = "js/bootstrap.bundle.min.js"></script>
</html>

Here is the code for main_content.php:

<main class = "container mt-5">
<div class = "display-none" id = "change_display">
	<div id = "change_avatar" class = "container">
	</div>
</div>	
</main>

Here is the JS code called from onclick function (which is in header) (no problem from calling a function), the problem is in that both class - "armen" and style doesn't work in my html, but div with form is created without any issues.

function changeAvatar() {
	document.getElementById('change_avatar').innerHTML += '<div class = "armen" style = "width:500px;height:500px;"><form action="" method="post" enctype="multipart/form-data"><input type="file" name="image"><input type="submit" value="Upload Image" name="avatar_btn"><input type="submit" value="Delete Image" name="del_avatar_btn"></form></div>'
}

答案1

得分: 0

你的代码在调用函数时似乎运行良好。已添加classstyle,如我在其周围放置的红色边框所示,通过该类选择并显示heightwidth已设置。

英文:

Your code seems to work just fine when I call the function. The class and style are added which is shown by the red border I put around it which selects by that class and shows that the height and width are set on it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function changeAvatar(){
    document.getElementById(&#39;change_avatar&#39;).innerHTML += `
&lt;div class = &quot;armen&quot; style = &quot;width:500px;height:500px;&quot;&gt;
  &lt;form action=&quot;&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
    &lt;input type=&quot;file&quot; name=&quot;image&quot;&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Upload Image&quot; name=&quot;avatar_btn&quot;&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Delete Image&quot; name=&quot;del_avatar_btn&quot;&gt;
  &lt;/form&gt;
&lt;/div&gt;`
}

changeAvatar();

<!-- language: lang-css -->

.armen {
  border: 1px solid red;
}

<!-- language: lang-html -->

&lt;div id=&quot;change_avatar&quot; class=&quot;container&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 02:48:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定