如何在jQuery中输入后删除文本?
的jQuery
<div id="date_filter_si">
<input type="radio" class="date_action" name="date_action" value="Y" checked=""> year
<input type="radio" class="date_action" name="date_action" value="W" checked=""> Weekly
</div>
预期输出
<div id="date_filter_si">
<input type="radio" class="date_action" name="date_action" value="Y" checked="">
<input type="radio" class="date_action" name="date_action" value="W" checked="">
</div>
$( "#date_filter_si:contains('Weekly')" ).remove();
答案 0 :(得分:2)
您可以过滤文本节点(element.nodeType == 3)然后删除。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">
<input type="radio" class="date_action" name="date_action" value="Y" checked="" onclick="set_date_filter('Y')"> year
<input type="radio" class="date_action" name="date_action" value="W" checked="" onclick="set_date_filter('W')"> Weekly
</div>
&#13;
'/user'
&#13;
答案 1 :(得分:2)
您可以使用Node.nextSibling
属性更改元素的下一个兄弟文本。
$("#date_filter_si > input")[0].nextSibling.textContent = "";
$("#date_filter_si > input")[0].nextSibling.textContent = "";
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">
<input type="radio" class="date_action"> year
</div>
如果你有多个输入和兄弟文本,请使用:
$("#date_filter_si > input").each(function(){
this.nextSibling.textContent = "";
});
$("#date_filter_si > input").each(function(){
this.nextSibling.textContent = "";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">
<input type="radio" class="date_action"> year
<input type="radio" class="date_action"> week
</div>
答案 2 :(得分:0)
我就是这样做的。
$( "#date_filter_si:contains('Weekly') input" ).each(function(){
var el = this.nextSibling;
if(el && el.nodeType === Node.TEXT_NODE) {
el.parentNode.removeChild(el);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">
<input type="radio" class="date_action" name="date_action" value="Y" checked="" onclick="set_date_filter('Y')"> year
<input type="radio" class="date_action" name="date_action" value="W" checked="" onclick="set_date_filter('W')"> Weekly
</div>