2016-04-16 13 views
0

私はこのようなマークアップを持っている:変更テキストは

<li data-id="1"> 
    <button type="button" class="btn btn-default btn-md" style="margin: .25em; padding: .25em;"> 
    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> 
    random text 
    <input type="hidden" name="global_filter[default_fields][][apple]" value="random text_again"> 
    </button> 
</li> 

は私が「何かを」テキスト「ランダムテキスト」を変更したいです。

私は、テキストノードを見つけることができています:

$li.find('button').first().contents().filter(function() { 
    return this.nodeType == 3; 
    }).text(); 

をしかし、どのように、私はテキストを変更できますか?私がtext('something else')のようなテキストに引数を渡すと、何も変わりません。

答えて

1

私はおそらく

<div id='changeableText'>Random Text</div>

のように、独自のコンテナ内にテキストを配置し、ちょうどこれを達成するために

$('#changeableText').text('New text!');

で直接アクセスします。

0

あなたはボタンのちょうどtext()を選択して、それを変更することができます

を更新しました。

注:これ以上のマークアップされていないテキストが1つ以上存在する場合、これは機能しません。例えば。

<li data-id="1"> 
    <button type="button" class="btn btn-default btn-md" style="margin: .25em; padding: .25em;"> 
    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> 
    random text 
    <input type="hidden" name="global_filter[default_fields][][apple]" value="random text_again"> 
    another random text 
    </button> 
</li> 

そうでなければ、彼女は 作業のデモです:このお試しください

var getNonMarkedUpText = $('button').text().replace(/^\s+|\s+$|\s+(?=\s)/g, ""); 
 
$('button').html($('button').html().replace(getNonMarkedUpText, 'something else'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<li data-id="1"> 
 
    <button type="button" class="btn btn-default btn-md" style="margin: .25em; padding: .25em;"> 
 
    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> 
 
    random text 
 
    <input type="hidden" name="global_filter[default_fields][][apple]" value="random text_again"> 
 
    </button> 
 
</li>

+0

これは、ボタン内の他のネストされた要素を取り除くの負の効果を持って、私は別の方法を見つけて、しばらくの答えを更新します...待っ – Donato

+0

。 – Roy

+0

今すぐ確認してください。コードを更新しました。 – Roy

0

:その方法

<li data-id="1"> 
    <button type="button" class="btn btn-default btn-md" style="margin: .25em; padding: .25em;"> 
    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> 
    <span id="txt1">random text</span> 
    <input type="hidden" name="global_filter[default_fields][][apple]" value="random text_again"> 
    </button> 
</li> 

を、あなたが行くことができます:

document.getElementById("txt1").innerText = "txet modnar"; 

それとも

$("#txt1").text("tandom rext"); 
関連する問題