JS在var中替换html中的id
JS replace id from html in var
我创建了一个javascript,在index.php文件中注入html片段。它需要将id替换为包含索引号的新id。这是我的type。txt:
<section id="banner" class="wow fadeInUp" style="position: relative; padding: 100px 0;">
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-1 col-sm-6">
<div class="block" style="padding-top: 10px;">
<h1 id="K0title" style="margin-bottom: 18px; line-height: 55px; color: #262b31; font-weight: 300;">
Loading...
</h1>
<p id="K0text" style="margin-bottom: 65px; font-size: 20px; color: #999; font-weight: 300;">
Loading...
</p>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="block" style="padding-top: 10px;">
<img id="K0image" class="app-img img-responsive" src="images/background 2.png" alt="">
</div>
</div>
</div>
</div>
</section>
这是我的javascript代码:
if(type == 1){
var html1 = new XMLHttpRequest();
html1.open('GET', 'js/txt/type1.txt', false);
html1.send();
allText = html1.responseText;
titleReplace = "K" + id + "title";
textReplace = "K" + id + "text";
imageReplace = "K" + id + "image";
alert(titleReplace);
allText.replace("K0title", titleReplace);
allText.replace("K0text", textReplace);
allText.replace("K0image", imageReplace);
document.getElementById(id).innerHTML = allText;
document.getElementById(titleReplace).innerHTML = title;
document.getElementById(textReplace).innerHTML = text;
document.getElementById(imageReplace).src = imageUrl;
}
不知何故,id保持K0…在index.php.
我想这和"
有关thx
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
相关文章:
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 使用html中的外部javascript进行数据验证
- JS验证ajax返回的html中的表单数据
- 如何使用jquery更改html中的背景颜色
- Brightcove获取/显示HTML中的当前视频标题和描述
- 如何关闭html中的iframe弹出窗口
- 计算HTML中的页数
- 通过Javascript将HTML中的电话号码与URL参数进行交换
- 是否可以从html中的javascript调用.vbs文件
- HTML中的垂直进度条
- HTML 中的 Javascript 不会执行
- 使用CSS嵌入HTML中的水平居中jQuery
- 从JavaScript中正确返回html中的特殊字符
- 访问HTML中的data-*属性并将其作为道具传递
- 从.js文件调用html中的js函数
- JS/jquery:我将如何遍历HTML中的每个图像
- 对javascript使用.html中的select id
- 引用nodejs服务器中html中的java脚本文件
- 可以't在Wordpress中从HTML中的JS文件调用函数
- 从HTML中的外部JS文件调用函数