我有这段代码:
<textarea class="mapCode">
< area shape="rect" coords="" href="http://www.sitehere" target="_self">
</textarea>
我如何使用jQuery将值插入/更新到coords属性中,即使它不是空的?
现在我正在使用......
return $(this).text().replace("coords=\"\"", 'coords="'+selection.x1+','+selection.y1+','+selection.x2+','+selection.y2+'"');
但当然只能使用一次,因为它总是在寻找一个空的coords=""
属性。
答案 0 :(得分:1)
您应该在替换调用中使用带通配符的正则表达式:
$(this).text().replace(/coords=".*"/gi, 'coords="..."');
这将同时使用empty和set coords属性。
答案 1 :(得分:0)
您可以将textarea内容包装在jQuery对象中,然后使用任何相关方法来更新它,例如:
var $content = $('<div/>').html($('.mapCode').val());
$content.find('area').attr('coords', selection.x1+','+selection.y1+','+selection.x2+','+selection.y2);
$('.mapCode').val($content.html());
答案 2 :(得分:-1)
<textarea class="mapCode">
<area id="myArea" shape="rect" coords="" href="http://www.sitehere" target="_self">
</textarea>
JavaScript的:
$(function () {
var myArea = $('#myArea');
myArea.attr('coords', selection.x1+','+selection.y1+','+selection.x2+','+selection.y2);
});