jQueryのappend()で文字列を追加後、
その文字列をreplace()で置き換え(削除)したいのですがうまくいきません。
変数を利用したいのでRegExpオブジェクトにグローバルマッチのフラグ (g)をつけたのですが
置換されません。
#####希望する動作
0. 「晴れ」をクリックするとpタグ内に「晴れ」が追加され、div#infoを表示する。
「晴れ」をクリック → <p>本日の天気は晴れです</p>
- div#info内のdeleteボタンをクリックするとdiv#infoが消えてpタグ内は**初期値「本日の天気は」**に戻す。
「消す」をクリック → </p>本日の天気は</p>
- 「雨」をクリックするとpタグ内に「雨」が追加され、div#infoを表示する。
「雨」をクリック → <p>本日の天気は雨です</p>
html
1<button id="sunny">晴れ</button> 2<button id="rainy">雨</button> 3 4<div id="info" style="display: none;"> 5<p>本日の天気は</p> 6<button id="delete">消す</button> 7</div>
javascript
1(function ($) { 2 3$(#sunny).click(function(){ 4weather = $(this).text(); 5weather_info = '<b>'+ weather + '</b>です'; 6 $(#info > p).append(weather_info); 7 $(#info).show(); 8}) 9 10$(#rainy).click(function(){ 11weather = $(this).text(); 12weather_info = '<b>'+ weather + '</b>です'; 13 $(#info > p).append(weather_info); 14 $(#info).show(); 15}) 16 17$(#delete).click(function(){ 18 $(#info).hide(); 19 $(#info > p).replace(new RegExp(weather_info,'g'),''); 20}) 21 22})(jQuery) 23
ご教授いただけますでしょうか。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー