gpt4 book ai didi

javascript - 使用 JavaScript 和 jQuery 生成 HTML - JavaScript 不执行?

转载 作者:行者123 更新时间:2023-11-28 06:50:19 25 4
gpt4 key购买 nike

跟随this帖子,我用 jQuery 重新创建了代码。 jsFiddle 说,一切都应该找到,我也找不到任何错误。这是代码:

function generatePost(title, time, text) {
var postCount = 0;

$('#postcontainer').append('<div></div>').attr('post_' + postCount).attr('class', 'content');
$('#post_' + postCount).append('<h3>' + title + '</h3>').attr('id', 'post_h3_' + postCount);
$('#post_h3_' + postCount).append('<span>' + time + '</span>');

var paragraphs = text.split("||");

for (var i = 0; i < paragraphs.length; i++) {
var paragraphCount = 0;
$('#post_' + postCount).append('<p>' + paragraphs[i] + '</p>').attr('id', 'post_p_' + postCount + '_' + paragraphCount);
paragraphCount++;
}

postCount++;
}

现在的问题可能是 JavaScript 甚至没有执行,我的 HTML 看起来像这样:

<head>
<?php include 'components/_head.php';?>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/jquery-migrate-1.2.1.min.js"></script>
<script src="js/postGen.js"></script>
<script type="application/javascript" src="postGen.js">
document.addEventListener('DOMContentLoaded', function() {
generatePost("Testing Title", "I don't know", "This is || a paragraph");
}
</script>
</head>
<body>
<header>
<?php include 'components/header.php';?>
</header>

<main>
<?php include 'components/main.php';?>
<div class="fullscreencontainer">
<img class="background" src="img/background.jpg">
<div class="title">
<h2>Der Bürger als Edelmann</h2>
<h4>von Moliére</h4>
</div>
</div>
<div class="container">
<div id="postcontainer">
/* ? */
</div>
<div class="sidebar">

</div>
</div>
</main>
<footer>
<?php include 'components/footer.php';?>
</footer>
</body>

文件结构:

index.php
js/jquery-1.11.3.min.js
js/jquery-migrate-1.2.1.min.js
js/postGen.js 此文件包含第一个代码框中编写的代码

Inspector 没有显示任何新内容。
控制台显示:

Syntax Error: Missing ) after argument list


编辑:最终代码:

    <script type="application/javascript">
$(document).ready(function() {
function generatePost(title, time, text) {
var postCount = 0;
$('#postcontainer').append('<div id=' + "post_" + postCount + '></div>')
$('#post_' + postCount).attr('class', 'content');
$('#post_' + postCount).append('<h3>' + title + '<span>' + time + '</span></h3>');

var paragraphs = text.split("||");
for (var i = 0; i < paragraphs.length; i++) {
$('#post_' + postCount).append('<p>' + paragraphs[i] + '</p>');
}
postCount++;
}

generatePost("Testing Title", "I don't know", "This is || a paragraph");
generatePost("Testing Title", "I don't know", "This is || a paragraph");
generatePost("Testing Title", "I don't know", "This is || a paragraph");
generatePost("Testing Title", "I don't know", "This is || a paragraph");
});
</script>

最佳答案

似乎您过早地调用了 generatePost() 函数(当文档加载时,但与此同时,这也是您加载 jQuery 时...为什么您会收到“)”丢失错误...)。

所以我将 HTML 更改为:

<head>
</head>
<body>
<main>
<div class="container">
<div id="postcontainer">

</div>
</div>
</main>
</body>

和 jQuery 到:

$(document).ready(function() {
function generatePost(title, time, text) {
var postCount = 0;
$('#postcontainer').append('<div id=' + "post_" + postCount + '></div>').attr('class', 'content');
$('#post_' + postCount).append('<h3>' + title + '</h3>').attr('id', 'post_h3_' + postCount);
$('#post_h3_' + postCount).append('<span>' + time + '</span>');
var paragraphs = text.split("||");

for (var i = 0; i < paragraphs.length; i++) {
var paragraphCount = 0;
$('#post_' + postCount).append('<p>' + paragraphs[i] + '</p>').attr('id', 'post_p_' + postCount + '_' + paragraphCount);
paragraphCount++;
}
postCount++;
}
generatePost("Testing Title", "I don't know", "This is || a paragraph");
});

只需调用 document.ready 末尾的函数即可。此外,如您所见,我还遇到了一些关于如何分配帖子 ID 的问题,因此最终也将其切换,用一些简单的字符串插值分配它。

干杯,

PG

关于javascript - 使用 JavaScript 和 jQuery 生成 HTML - JavaScript 不执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33814490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com