尝试创建一个遍历每个跨度的脚本,以检查它:是否包含与页面的h1元素相同的文本。
$(document).ready(function() {
var partTitle = $("#product_title").text();
$("span").each(function(i) {
if ($(this).text().is(":contains('" + partTitle + "')")) {
$(this).css = ("display", "none");
} else {
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 class="page_headers" id="product_title">5/8 Other Stuff</h1>
<label class="radio-option">
<span>5/8</span>
</label>
<label class="radio-option">
<span>1/8</span>
</label>
<label class="radio-option">
<span>1/2</span>
</label>
发布于 2018-12-12 18:11:30
如果要检查头字符串在每种情况下是否包括 span
文本,请检查下一个示例:
$(document).ready(function()
{
var partTitle = $("#product_title").text();
$("span").each(function(i)
{
if ( partTitle.includes($(this).text()) )
{
$(this).fadeOut(2000);
// Alternatively you can use hide, like on next line.
// fadeOut() was used because it gives more time to
// see it working.
//$(this).hide();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 class="page_headers" id="product_title">5/8 Other Stuff</h1>
<label class="radio-option">
<span>5/8</span>
</label>
<label class="radio-option">
<span>1/8</span>
</label>
<label class="radio-option">
<span>1/2</span>
</label>
在要检查span
文本是否包括header
文本的情况下,可以实现类似的逻辑:
$(document).ready(function()
{
var partTitle = $("#product_title").text();
$("span").each(function(i)
{
if ( $(this).text().includes(partTitle) )
{
$(this).fadeOut(2000);
// Alternatively you can use hide, like on next line.
// fadeOut() was used because it gives more time to
// see it working.
//$(this).hide();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 class="page_headers" id="product_title">5/8 Other Stuff</h1>
<label class="radio-option">
<span>5/8 Other Stuff</span>
</label>
<label class="radio-option">
<span>1/8</span>
</label>
<label class="radio-option">
<span>1/2</span>
</label>
发布于 2018-12-12 18:15:13
那么,您想看看跨域中的文本是否完全匹配,还是在您的partTitle中有部分呢?
<h1 class="page_headers" id="product_title">5/8 Other Stuff</h1>
<label class="radio-option">
<span>5/8</span>
</label>
<label class="radio-option">
<span>1/8</span>
</label>
<label class="radio-option">
<span>1/2</span>
</label>
如果您只是检查您的跨partTitle中是否包含任何文本,那么您可以:
$(document).ready(function() {
var partTitle = $("#product_title").text();
$("span").each(function( i ) {
console.log(partTitle.includes($(this).text()));
if ($(this).text().includes(partTitle)) {
console.log('hi');
$(this).css = ("display", "none");
} else {
}
});
});
console.log包含如何检查其中任何一个在h1中是否包含一些文本,因此5/8跨度是真的。
上述if中的当前代码检查span的文本是否完全匹配。
发布于 2018-12-12 18:08:22
您可以使用includes()
函数,例如:
$(document).ready(function() {
var partTitle = $("#product_title").text();
$("span").each(function(i) {
if ($(this).text().includes(partTitle)) {
$(this).css("color", "green");
} else {
$(this).css("color", "red");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 class="page_headers" id="product_title">5/8 Other Stuff</h1>
<label class="radio-option">
<span>5/8</span>
</label>
<label class="radio-option">
<span>5/8 Other Stuff</span>
</label>
<label class="radio-option">
<span>1/8</span>
</label>
<label class="radio-option">
<span>1/2</span>
</label>
https://stackoverflow.com/questions/53748740
复制相似问题