您好我想弄清楚如何检查特定字符串是否存在,如果该字符串不存在,请更改另一个节点的文本。
以下显示了我到目前为止所做的工作。我遇到的问题是,我执行文本更改的条件根本不起作用。不可否认,这是我第一次使用indexOf(),所以我不确定我是否正确这样做。
// Check to see if there are non-refundable rates
// If they are NOT refundable DO NOT change the name
var taoIGCOR = jQuery('.rateInfoContainer[data-rate-code="RATECODE"]');
var taoRateBullet = jQuery('.rateBullet').text().indexOf('Non Refundable') <= -1;
if (jQuery(taoRateBullet).text().indexOf('Non Refundable') >= 1) {
// If 'Non Refundable' text IS NOT present, change it to this...
jQuery(taoIGCOR).text("THIS IS THE NEW RATE NAME");
alert("Change the rate name");
} else {
// If 'Non Refundable' text IS present don't do anything
alert("There are refundable rates here....don't do anything.");
}
&#13;
.rateInfoContainer {
border: 1px solid red;
padding: 15px;
margin-bottom:10px;
}
.rateName {
border: 1px solid blue;
padding: 15px;
}
.rateBullets {
border: 1px solid green;
padding: 15px;
}
.color {
background: gray;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rateInfoContainer" data-rate-code="RATECODE">
<div class="rateName">
This text does not change
<div class="rateDescription">
<ul class="rateBullets">
<li>Non Refundable</li>
</ul>
</div>
</div>
</div>
<div class="parent rateInfoContainer">
<div class="textTarget rateName">
This is the target text
<div class="rateDescription">
<ul class="nested rateBullets">
<li>String not present...text should change</li>
</ul>
</div>
</div>
</div>
<div class="parent rateInfoContainer">
<div class="textTarget rateName">
This text does not change
<div class="rateDescription">
<ul class="nested rateBullets">
<li>Non Refundable</li>
</ul>
</div>
</div>
</div>
&#13;
感谢所有帮助/指导!
答案 0 :(得分:1)
indexOf()
返回字符串中字符串的索引位置,从位置0
开始。如果找不到子字符串,则返回-1
。
所以,如果你想知道“不可退款”是否在.rateBullet
中,那么:
var taoRateBullet = jQuery('.rateBullet').text().indexOf('Non Refundable') <= -1;
应该是这样的:
var taoRateBullet = jQuery('.rateBullet').text().indexOf('Non Refundable') >= -1;
而且,如果您想知道“{不可退款”是否不在taoRateBullet
中,那么:
if (jQuery(taoRateBullet).text().indexOf('Non Refundable') >= 1) {
应该是这样的:
if (jQuery(taoRateBullet).text().indexOf('Non Refundable') === -1) {
现在,您还遇到了其他一些问题,因此请查看以下评论以获取详细信息:
// It's a best-practice to name variables that hold JQuery wrapped sets
// with a leading $ to remind you that they are not standard DOM objects.
// You can also access JQuery with just "$" instead of "jQuery"
var $taoIGCOR = $('.rateInfoContainer[data-rate-code="RATECODE"]');
// Check to see if there are non-refundable rates
// If they are NOT refundable DO NOT change the name
if ($('.rateBullet').text().indexOf('Non Refundable') === -1) {
// If 'Non Refundable' text IS NOT present, change it to this...
$taoIGCOR.text("THIS IS THE NEW RATE NAME");
alert("Change the rate name");
} else {
// If 'Non Refundable' text IS present don't do anything
alert("There are refundable rates here....don't do anything.");
}
.rateInfoContainer {
border: 1px solid red;
padding: 15px;
margin-bottom:10px;
}
.rateName {
border: 1px solid blue;
padding: 15px;
}
.rateBullets {
border: 1px solid green;
padding: 15px;
}
.color {
background: gray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rateInfoContainer" data-rate-code="RATECODE">
<div class="rateName">
This text does not change
<div class="rateDescription">
<ul class="rateBullets">
<li>Non Refundable</li>
</ul>
</div>
</div>
</div>
<div class="parent rateInfoContainer">
<div class="textTarget rateName">
This is the target text
<div class="rateDescription">
<ul class="nested rateBullets">
<li>String not present...text should change</li>
</ul>
</div>
</div>
</div>
<div class="parent rateInfoContainer">
<div class="textTarget rateName">
This text does not change
<div class="rateDescription">
<ul class="nested rateBullets">
<li>Non Refundable</li>
</ul>
</div>
</div>
</div>