javascript - 根据 div:contains 值更改数据集属性
问题描述
我正在尝试根据相应 div 的内容更改 a 标签的 data-target 属性。我已经测试了几种不同的途径,并认为这是最干净的。但是,由于这是在修改多个条目,因此只有第一个条目的数据目标正在更新,并且它正在被修改为市场,而不是负担得起的。.html 和 .css 更改按预期工作。只有导致问题的数据目标元素。您可以在下面看到我尝试过的其他变体评论。
jQuery(document).ready(function ($) { //replaces all instances of $ with jquery for Wordpress
$(".unit-rate-mapped div").each(function () {
if ($(this).is(':contains("mk")')) { //Only Market Rate units codes will contain mk
$(this).html('Market Rate'); //Change the HTML value to Market Rate
$(this).css({ "background": "rgba(4,57,99,.1)", "color": "#043963" });
$('#unit-qual-link').attr('data-target', '.fusion-modal.form-market'); //modify the data-target attribute of the pre-Qualify link to link to the market form
//document.getElementById("unit-qual-link").dataset.target = ".fusion-modal.form-market";
} else { //Anything and everything else goes to orange
$(this).html('Affordable'); //Change the HTML value to Affordable
$(this).css({ "background": "rgba(229,104,53,.1)", "color": "#e56835" }); //Change the background color and type to orange
$('#unit-qual-link').attr('data-target', '.fusion-modal.form-affordable'); //modify the data-target attribute of the pre-Qualify link to link to the afforrdable form
//document.getElementById("unit-qual-link").dataset.target = ".fusion-modal.form-affordable"; //modify the URL of the Pre-Qualify link to link to the affordable form
}
});
//Tried this variation as well
// if ($(".unit-rate-mapped div:contains('Market Rate')")) {
// $('#unit-qual-link').attr('data-target', '.fusion-modal.form-market');
// } else {
// $('#unit-qual-link').attr('data-target', '.fusion-modal.form-affordable');
// }
});
这是我要修改的 HTML 示例:
<!--Market Example-->
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">mk1234</div>
</div>
<div class="unit-qualifications">
<a id="unit-qual-link" class="fusion-modal-text-link" data-toggle="modal" data-target=".fusion-modal.form-market" href="#">Pre-Qualify</a></div>
</div>
<!--Affordable Example-->
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">1b1b60</div>
</div>
<div class="unit-qualifications">
<a id="unit-qual-link" class="fusion-modal-text-link" data-toggle="modal" data-target=".fusion-modal.form-market" href="#">Pre-Qualify</a>
</div>
</div>
编辑以包含@freedomn-m 的建议,但数据属性返回空白:
jQuery(document).ready(function ($) { //replaces all instances of $ with jquery for Wordpress
$(".unit-rate-mapped div").each(function () {
if ($(this).is(':contains("mk")')) { //Only Market Rate units codes will contain mk
$(this).html('Market Rate'); //Change the HTML value to Market Rate
$(this).css({ "background": "rgba(4,57,99,.1)", "color": "#043963" });
$('#unit-qual-link').data('target', '.fusion-modal.form-market'); //modify the data-target attribute of the pre-Qualify link to link to the market form
//document.getElementById("unit-qual-link").dataset.target = ".fusion-modal.form-market";
} else { //Anything and everything else goes to orange
$(this).html('Affordable'); //Change the HTML value to Affordable
$(this).css({ "background": "rgba(229,104,53,.1)", "color": "#e56835" }); //Change the background color and type to orange
$('#unit-qual-link').data('target', '.fusion-modal.form-affordable'); //modify the data-target attribute of the pre-Qualify link to link to the afforrdable form
//document.getElementById("unit-qual-link").dataset.target = ".fusion-modal.form-affordable"; //modify the URL of the Pre-Qualify link to link to the affordable form
}
});
//Tried this variation as well
// if ($(".unit-rate-mapped div:contains('Market Rate')")) {
// $('#unit-qual-link').attr('data-target', '.fusion-modal.form-market');
// } else {
// $('#unit-qual-link').attr('data-target', '.fusion-modal.form-affordable');
// }
});
空白数据目标的结果:
<a id="unit-qual-link" class="fusion-modal-text-link" data-toggle="modal" data-target="" href="#" data-uw-styling-context="true">Pre-Qualify</a>
解决方案
好吧,我找到了解决方案。简而言之,我在 HTML 层次结构中进行逻辑调用为时已晚。我没有正确针对.unit-rate-map
班级的孩子,因为它没有孩子。这是清理后的 HTML 以供参考:
<!--Market Example-->
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">mk1234</div>
</div>
<div class="unit-qualifications">
<a class="fusion-modal-text-link unit-qual-link" data-toggle="modal" data-target=".fusion-modal.form-affordable" href="#">Pre-Qualify</a>
</div>
</div>
<!--Affordable Examples-->
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">1b1b60</div>
</div>
<div class="unit-qualifications">
<a class="fusion-modal-text-link unit-qual-link" data-toggle="modal" data-target=".fusion-modal.form-affordable" href="#">Pre-Qualify</a>
</div>
</div>
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">2b1b60</div>
</div>
<div class="unit-qualifications">
<a class="fusion-modal-text-link unit-qual-link" data-toggle="modal" data-target=".fusion-modal.form-affordable" href="#">Pre-Qualify</a>
</div>
</div>
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">3b1b60</div>
</div>
<div class="unit-qualifications">
<a class="fusion-modal-text-link unit-qual-link" data-toggle="modal" data-target=".fusion-modal.form-affordable" href="#">Pre-Qualify</a>
</div>
</div>
<!--Market Example-->
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">mk12345</div>
</div>
<div class="unit-qualifications">
<a class="fusion-modal-text-link unit-qual-link" data-toggle="modal" data-target=".fusion-modal.form-affordable" href="#">Pre-Qualify</a>
</div>
</div>
<!--Affordable Example-->
<div class="essential-grid-unit-row-grid row-1">
<div class="unit-rate-mapped">
<div class="unit-rate-map">1b1b60</div>
</div>
<div class="unit-qualifications">
<a class="fusion-modal-text-link unit-qual-link" data-toggle="modal" data-target=".fusion-modal.form-affordable" href="#">Pre-Qualify</a>
</div>
</div>
以及相应的 JQuery:
jQuery(document).ready(function ($) { //replaces all instances of $ with jquery for Wordpress
$(".essential-grid-unit-row-grid").each(function () {
if ($(this).find(".unit-rate-map").is(':contains("mk")')) { //Only Market Rate units codes will contain mk
$(this).find(".unit-rate-map").html('Market Rate'); //Change the HTML value to Market Rate
$(this).find(".unit-rate-map").css({ "background": "rgba(4,57,99,.1)", "color": "#043963" });
$(this).find(".unit-qual-link").attr("data-target", ".fusion-modal.form-market");
} else { //Anything and everything else goes to orange
$(this).find(".unit-rate-map").html('Affordable'); //Change the HTML value to Affordable
$(this).find(".unit-rate-map").css({ "background": "rgba(229,104,53,.1)", "color": "#e56835" }); //Change the background color and type to orange
}
});
});
我有针对性.essential-grid-unit-row-grid
,然后用来在循环$(this).find()
中调用该类的特定子级。.each()
这是一个工作小提琴:https ://jsfiddle.net/3mqhudca/4/
推荐阅读
- java - 通过在线每月订阅限制我的 java 应用程序
- r - 获取具有缺失 (NA) 值的列的平均值
- python - 如何每分钟通过 API 下载一个 json 文件而不会被 asyncio 停止?
- r - 控制 n 个重叠区域的 alpha 混合/不透明度
- r - 在 R 中建模响应行为
- excel - 如何修复 vba for excel 中的运行时错误“430”和其他自动化错误
- python - 带有条件的 concat 2 数据帧
- javascript - 在 HTML 中显示表情符号
- python - 如何按键排序有组织的字典
- selenium - VS2017 中的硒 - 找不到元素。点不可点击