【发布时间】:2018-07-31 17:56:41
【问题描述】:
我有一个类似这样的 HTML 网页。 我想从类样本选项中隐藏背景,并在 div 中呈现选项标签。
但我无法获得选项标签。
$(document).ready(function() {
if ($('div.swatch-option').hasClass('color')) {
console.log($(this).attr('option-label'));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="swatch-attribute swatch-layered color" attribute-code="color" attribute-id="93">
<div class="swatch-attribute-options clearfix">
<a href="#" aria-label="Black" class="swatch-option-link-layered">
<div class="swatch-option color " tabindex="-1" option-type="1" option-id="49" option-label="Black" option-tooltip-thumb="" option-tooltip-value="#000000" style="background: #000000 no-repeat center; background-size: initial;"></div>
</a>
<a href="#" aria-label="Red" class="swatch-option-link-layered">
<div class="swatch-option color " tabindex="-1" option-type="1" option-id="50" option-label="Red" option-tooltip-thumb="" option-tooltip-value="Red" style="background: Red no-repeat center; background-size: initial;"></div>
</a>
</div>
</div>
这是我正在尝试的代码。但它显示未定义。页面上有更多带有class = "swatch-attribute swatch-layered" 的div,同样还有更多带有swatch-attribute-options 和swatch-option 类的div。所以有点复杂。谁能帮我获取值,以便我禁用背景并将值设置为选项标签
【问题讨论】:
-
我会这样做:$('div.swatch-option').each(function() { if($(this).hasClass('color')){ console.log($( this).attr('option-label')); }});
-
感谢大家这么快的帮助,批准的答案是第一个出现的。
标签: javascript jquery html