我有以下代码:
<div class="sc-gPEVay JHVSz" data-locator="category" data-values="9AAF500002"><button class="sc-jWBwVP mdc-ripple-surface ebJHFF" data-locator="category-header" style="align-items: stretch; background-color: rgba(0, 0, 0, 0); border-color: rgba(0, 0, 0, 0); border-style: none; box-shadow: none; color: rgb(114, 114, 114); cursor: pointer; display: flex; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: 13px; line-height: inherit; font-family: inherit; margin: 0px; outline: none; padding: 0px; text-decoration: none; width: 100%;"><i aria-hidden="true" class="sc-brqgnP material-icons_mi_2breL5T5PilQnhNwuNJjVT material-icons_mi-arrow-drop-down_1Yg8_MNgX_6a8ed5ZighLw ygVYg" data-locator="category-expander" style="height: 24px; margin-right: 12px; transition: transform 175ms ease 0s; transform: rotate(-90deg);"></i><div class="sc-cMljjf bIKLMh" data-locator="category-label" style="font-family: Verdana, Arial, sans-serif; margin-top: 4px; min-height: 24px; text-align: left;"> My Text </div></button><div class="sc-jDwBTQ kFDQOY" data-locator="category-children" style="margin-left: 10px;"></div></div>
我想要实现的是无论点击落在何处都返回“我的文本”文本。
我试过了:
function (){
var $clickElement = $({{Click Element}});
var $x = $clickElement.closest('i').find('div');
return $x.text();
PS。我不能/不想使用这些类(他们可以改变)
你为什么不用attribute selector。
var $clickElement = $({{Click Element}});
var clickedOnMyText = $clickElement.attr('data-locator') === 'category-label';
var $x;
if(clickedOnMyText) {
$x = $clickElement;
} else {
$x = $clickElement.find('[data-locator="category-label"]');
}
return $x.text();