替换 HTML 中的 JS 内容
Replacing JS content in HTML?
我正在尝试将.bio__content h4
文本替换为我在JS文件中创建的newLinks
内容。
我正在尝试使用 forEach
函数显示页面的newLinks
,但它对我不起作用。我不确定我做错了什么,我想用newLinks
替换.bio__content h4
内容,所以我使用了replace()
函数。
这不对吗?
.HTML
<div class="bio__content">
<h1 itemprop="name">Name</h1>
<div>
<h4><%- profile.designations %></h4>
</div>
<div>
.JS
var designation = JSContext.profile.designations.split(", ");
// designation = ['CAP', 'AEA', 'AAA']
var newLinks = designation.map(function(x) {
return "<a class='modal'>" + x + "</a>" });
// newLinks = ["<a class='hey'>CAP</a>", "<a class='hey'>AEA</a>", "<a class='hey'>AAA</a>"]
newLinks.forEach(function (e) {
$(".bio__content").text().replace(".bio__content h4", e);
});
替换函数是一个字符串函数,它将字符串中的某个值替换为另一个值。你应该使用javascript向文档添加html的函数是$.html()。此外,如果您使用 join 函数将所有链接连接到一个 html 字符串并将其添加到文档中,则会更容易。
var designation = //JSContext.profile.designations.split(", ");
designation = ['CAP', 'AEA', 'AAA']
var newLinks = designation.map(function(x) {
return "<a class='modal'>" + x + "</a>" });
// newLinks = ["<a class='hey'>CAP</a>", "<a class='hey'>AEA</a>", "<a class='hey'>AAA</a>"]
var linksString = newLinks.join("'n");
$(".bio__content h4").html(linksString);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bio__content">
<h1 itemprop="name">Name</h1>
<div>
<h4><%- profile.designations %></h4>
</div>
<div>
相关文章:
- 使用node.js服务器中的evernote api获取note内容
- D3.js生成有效的SVG,但不显示任何内容
- 根据Angular.JS上一次的内容禁用选择
- 如何正确地使这个js片段内容不可知
- 将*.js文件的内容放入Object中
- 导致内容安全策略(CSP)冲突错误的本地jquery.js文件
- HTML 5 和 3.js 代码不会在网页上显示任何内容
- js.erb文件未呈现其内容
- 当数据库中的某些内容发生变化时调用HTTP方法Meteor.js
- 如何使用JS/jQuery在另一个网站上显示iframe中的特定内容
- 在Asp.net的空网站中,如何改变"Web.config”;使内容(.js)文件显示在浏览器中,而不是下载
- textarea根据内容js或jquery设置高度
- 有条件下载页面内容(js,css)
- 如何在Chrome扩展名的内容JS文件中包含其他JavaScript文件
- Cordova应用程序自动同步/更新Web内容(.js文件),如果更新版本可用,否则使用缓存版本
- 为什么IE 10&11可以't加载GZiped内容(js和css)
- 如何在页面加载后更改内容(JS)
- Div改变高度,当用户点击内容JS
- 显示对象内容- JS/jQuery
- 防止在回旋镖完成之前执行 html 内容.js