[英]Find and replace specific text with new div element containing that text in jQuery
I've got products description as text, which I can wrap into any container, but that's all what shop's template allows me to do: 我已经将产品描述描述为文本,可以将其包装到任何容器中,但这就是商店模板所允许的全部操作:
<div class="descriptionContainer">
Some text description, some text description.
Some text description, some text description.
Some text description, some text description.
Width: 80, Depth: 39, Height: 190
</div>
How can I use jQuery to replace dimensions string into: 如何使用jQuery 将尺寸字符串替换为:
<div class="productsDimensions">
<div class="row">
<span class="name">Width</span>
<span class="value">80</span>
</div>
<div class="row">
<span class="name">Depth</span>
<span class="value">39</span>
</div>
<div class="row">
<span class="name">Height</span>
<span class="value">190</span>
</div>
</div>
I have to do this automatically, because e-shop application osCommerce that I'm using doesn't come with feature that allows specifying more product info than name, price and description. 我必须自动执行此操作,因为我正在使用的e-shop应用程序osCommerce没有提供允许指定比名称,价格和说明更多的产品信息的功能。
Thanks for any tips! 感谢您的提示!
Sorry but I have to be more specific to not mislead you. 抱歉,但我必须更加具体,不要误导您。
What I got from shop template is: 我从商店模板中得到的是:
<div class="descriptionContainer">
Some text description, some text description.
Some text description, some text description.
Some text description, some text description.
Width: 80, Depth: 39, Height: 190
</div>
So I have to find the Width, Depth, Height values first. 所以我必须先找到Width,Depth,Height值。
assuming you have Width: 80, Depth: 39, Height: 190
in an element (ie. a div, paragraph, etc.): 假设您在一个元素(例如div,段落等)中具有
Width: 80, Depth: 39, Height: 190
:
var plain = $('#textContainerId').html();
var newContent = jQuery('<div class="productsDimensions" />');
var rows = plain.split(',');
for (var i = 0; i < rows.length; i++) {
var columns = rows[i].split(':');
var name = columns[0];
var val = columns[1];
jQuery('<div class="row"><span class="name">' + name + '</span><span class="value">' + val + '</span></div>').appendTo(newContent);
}
$('#textContainerId').replaceWith(newContent);
To answer your edited question, you can use something like this: 要回答您编辑过的问题,您可以使用以下方法:
var plain = $('.descriptionContainer').html();
plain = plain.match(/(\w+:\s*\d+)/g);
var newContent = jQuery('<div class="productsDimensions" />');
$(plain).each(function(index, value) {
var columns = value.split(':');
var name = columns[0];
var val = columns[1];
jQuery('<div class="row"><span class="name">' + name + '</span><span class="value">' + val + '</span></div>').appendTo(newContent);
});
$('.descriptionContainer').html($('.descriptionContainer').html().replace(/(\w+:\s*\d+,?\s*)/g, '')).append(newContent);
Not pretty, but you can do that. 不漂亮,但是你可以做到。
http://jsfiddle.net/RqEyJ/ http://jsfiddle.net/RqEyJ/
<div id="container">
</div>
var string = "Width: 80, Depth: 39, Height: 190";
var dimensions = string.split(",");
$("#container").append('<div class="productsDimensions">');
$.each(dimensions, function(index, value) {
var pair = value.split(":");
$("#container").append('<div class="row"><span class="name">'+$.trim(pair[0])+'</span>');
$("#container").append('<span class="value">'+$.trim(pair[1])+'</span></div>');
});
$("#container").append('</div>');
Something like this: 像这样:
<script type="text/javascript">
var html = $('<div class="productsDimension">');
var data = "Width: 80, Depth: 39, Height: 190".split(',');
$.each(data, function (i, o) {
var row = $('<div class="row">');
html.append(row);
row.append('<span class="name">' + $.trim(o.split(':')[0]) + '</span><span class="value">' + $.trim(o.split(':')[1]) + '</span>')
});
$('body').append(html);
</script>
Try this: 尝试这个:
<script type="text/javascript">
(function ($) {
var properties = "Width: 80, Depth: 39, Height: 190".split(','),
wrapper = $('<div class="productsDimensions"></div>');
$(properties).each(function (i, elem) {
var prop = this.split(':');
wrapper.append($('<div class="row"></div>')
.append($('<span class="name"></span>').text(prop[0]))
.append($('<span class="value"></span>').text(prop[1])));
});
})(jQuery);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.