从属性中获取数据并使用它来替换其他文本
Get data from attribute and use it to replace other text
我正在构建一个图例-当用户单击一个图标时,我想从"data-title"属性中抓取数据,然后使用该文本来替换"p.map-legend-key__copy",该文本目前表示"Marker text"。
这是目前为止我的代码…
HTML:<div class="map-legend">
<ul class="map-legend-list cf">
<li class="map-legend-list__item legend-1" data-title="legend one"><i class="fa fa-map-marker"></i></li>
<li class="map-legend-list__item legend-2" data-title="legend two"><i class="fa fa-map-marker"></i></li>
<li class="map-legend-list__item legend-3" data-title="legend three"><i class="fa fa-map-marker"></i></li>
<li class="map-legend-list__item legend-4" data-title="legend four"><i class="fa fa-map-marker"></i></li>
<li class="map-legend-list__item legend-5" data-title="legend five"><i class="fa fa-map-marker"></i></li>
</ul>
<div class="map-legend-key">
<p class="map-legend-key__copy">Marker text</p>
</div>
</div><!-- map-legend -->
jQuery: $('.map-legend-list__item').click(function() {
var legendText = $(this).attr('data-title');
$('p.map-legend-key__copy').replace('Marker text', legendText);
});
Thnaks,杰克。
应该有
var legendText = $(this).data('title');
如果你想替换整个文本
$('p.map-legend-key__copy').text(legendText);
如果您只想替换Marker text
$('p.map-legend-key__copy').text($('p.map-legend-key__copy').text().replace('Marker text', legendText));
这可以是另一种方式:
var legendText = $(this).data('title');
$(".p.map-legend-key__copy").html(function(i,t){
return t.replace('Marker text', legendText)
});
相关文章:
- 将href中的图像替换为其他元素中的图像
- 以编程方式将fabric-js-canvas的图像替换为hq图像,并重新计算其他对象的坐标和大小
- 用空格替换单引号和其他符号
- 追加子数据 URI,替换 IE 中的其他对象
- 在HTML文本框中查找某些子字符串,然后用其他字符串替换它们
- 用图像和其他内容替换动态内容
- Javascript:用其他字符替换反斜杠和双引号
- 检查是否在一定时间间隔内加载了多个图像;如果没有,则替换为其他 URL
- CKEDITOR 将 替换为 和其他标签
- 解释$apply和$eval |我可以用其他功能替换它们吗 |AngularJS.
- JavaScript 用具有特定 ID 的正则表达式 img 标记替换,但其他属性可以是任何属性
- 在包含其他标签的标签中,将一个文本替换为另一个文本
- 用字符串替换其他变量的字符串
- 为什么使用替换来取消转义 html 标签也会去除所有其他 html 标签
- 在特定日期将基本图像替换为其他图像.可能
- 使用正则表达式在字符串中查找并替换为其他样式
- 通过jQuery替换数字,而不更改样式中的数字或其他所有内容
- 替换其他两个符号之间的所有符号
- 从属性中获取数据并使用它来替换其他文本
- 对span元素执行JavaScript循环,检查它们的值并替换其他节点边框颜色