我有这个jquery代码来搜索文本并替换它。我有一个datalist里面的div。此jquery将仅编辑列表中的前两个项目。我怎么能让它搜索该文本的所有列表和/或整个页面并替换它。
<script type="text/javascript">
$(function () {
var elem = $("#details").html(); // fetch the HTML from the #foo ID
var elem2 = $("#details2").html();
var bath = "Bathroom"; // text to replace
var bedroom = "Bedroom"; // text to replace
var replaceBath = "Bath"; // text to replace with
var replaceBed = "Bed"; // text to replace with
elem = elem.replace(bath, replaceBath); // replace & update string
elem = elem.replace(bedroom, replaceBed); // replace & update string
elem2=elem2.replace(bath,replaceBath);
elem2=elem2.replace(bedroom,replaceBed);
$("#details").html(elem); // apply updated string to #foo
$("#details2").html(elem2);
alert(elem2);
});
</script>
我在数据主义者中有这两个div。
<div class="col-md-3">
<div class="details" id="details1"><%#DataBinder.Eval(Container.DataItem,"Bedrooms", "{0:n0}") %> <%#DataBinder.Eval(Container.DataItem,"Bathrooms", "{0:n1}") %> Single Family Home</div>
</div>
<div class="col-md-3">
<div class="details" id="details2"><%#DataBinder.Eval(Container.DataItem,"Bedrooms", "{0:n0}") %> <%#DataBinder.Eval(Container.DataItem,"Bathrooms", "{0:n1}") %> Single Family Home</div>
</div>
答案 0 :(得分:0)
jQuery Contains() selector应该能够帮助识别页面上包含搜索字符串的任何元素。通过使用*
选择器,我们告诉jQuery查看DOM中的每个标记。
var matches = $( "*:contains(" + stringToLookFor + ")" );
答案 1 :(得分:0)
使用以下各项尝试以下内容:
$(function () {
var bath = "Bathroom"; // text to replace
var bedroom = "Bedroom"; // text to replace
var replaceBath = "Bath"; // text to replace with
var replaceBed = "Bed"; // text to replace with
$(".details").each(function(){
var elem = $(this).html().replace(bath, replaceBath).replace(bedroom,replaceBed);
$(this).html(elem);
});
});