-3

我有一个像这样结构的 div <div class="pin">CODE# 1234</div>,现在如何使用 jquery 用我的新值替换“1234”?

4

7 回答 7

3

你可以做

$('.pin').text(function(idx, value){
    return value.replace(/\d+$/, 'newvalue')
})

演示:小提琴

于 2013-08-13T04:57:42.563 回答
1

用这个。

$(function(){ $('.pin').text('').text('1234') });

于 2013-08-13T06:32:16.850 回答
1

一个简单的解决方案是

function changePinValue(pin) {
   $('.pin').html("CODE# "+pin);
}

快乐编码..

于 2013-08-13T08:42:07.260 回答
1

使用.text().replace("1234", "new Val")

var oldText = $(".pin").text();
$(".pin").text(oldText.replace("1234", "New Value"));

JSFIDDLE

另一种解决方案是使用跨度:

<div class="pin">CODE# <span class="value">1234<span></div>

和js:

$(".value").text("newValue");
于 2013-08-13T04:57:48.930 回答
1

试试replace喜欢

$(".pin").text().replace("1234", "MYValue");

或尝试喜欢(可能有效但不确定)

var txt = $('.pin').text();
txt_arr = txt.split(' ');    //txt.split('#'); even
txt_arr[1] = newValue;
$('.pin').text(txt_arr);
于 2013-08-13T04:58:21.633 回答
1

尝试这个

<div class="pin">CODE# <span class="code_value">1234<span></div>

$(".pin").find('.code_value').text().replace("1234", "New Value");

或者

$(".pin").find('.code_value').html("New Value");
于 2013-08-13T05:01:24.023 回答
1

一个函数怎么样:

function replaceCode(codeToFind, codeToReplace) {
    $("div:contains('CODE# "+codeToFind+"')").text(function (_, value) {
        return value.replace('CODE# '+codeToFind, 'CODE# '+codeToReplace);
    });
}

replaceCode(1234, 9999);

演示小提琴在这里

例如上面的开始 HTML:

<div class="pin">CODE# 1234</div>

生成的 HTML:

<div class="pin">CODE# 9999</div>
于 2013-08-13T05:03:33.003 回答