Javascript或jQuery选择器从`a`然后到很多div使用类

时间:2017-03-04 09:07:50

标签: javascript jquery html

我有像这样的HTML

project {
    key: "ads_evenflow.opt"
    value {
        name: ads_evenflow.opt
        state: COMPLETE
        result: PASSED
    }
}
project {
    key: "android.opt"
    value {
        name: android.opt
        state: COMPLETE
        result: PASSED
    }
}

现在,我想使用<a href="/portal/product/62157993" id="ember1708" class="product-items__item ember-view"> <div id="ember1709" class="product-item product-view-item ember-view"> <div class="product-item__info"> <div class="product-item__name"> <span data-ember-action="" data-ember-action-1712="1712">Pashmina Denim</span> </div> <div class="product-item__promote"> <!----> <div id="ember1713" class="product-boost-button ember-view"> <div id="ember1714" class="shopee-button shopee-button--inactive shopee-button--medium ember-view"> Promote </div> </div> </div> </div> </div> </a> 选择器仅使用CLASS单击DIV with TEXT“Promote”,而不是使用ID

我尝试使用以下代码,但在我的情况下无效

a href="/portal/product/62157993"

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果您想选择$('input[name="switch"]').change(function() { var checked = $(this).is(':checked'); $('input[name="switch"]').prop('checked',false); var arr = ["brand", "fuel", "body"]; $.each( arr, function( i, val ) { var type = $('input[name="' + val + '"]').prop("type"); if (type == 'checkbox') { $('input[name="' + val + '"]').each(function(index, value){ var oldInput = $(value); if(index == 0){ $(value).replaceWith( $('<input type="radio" />'). prop("value", oldInput.prop("value")). prop("name", oldInput.prop("name")). prop("id", oldInput.prop("id")). prop('checked',true) ) } else { $(value).replaceWith( $('<input type="radio" />'). prop("value", oldInput.prop("value")). prop("name", oldInput.prop("name")). prop("id", oldInput.prop("id")) ) } }); }; }); if(checked) { $(this).prop('checked',true); var id = $(this).prop("id"); $('input[name="' + id + '"]').each(function(index, value){ var oldInput = $(value); var checked2 = $(value).is(':checked'); if(checked2) { $(value).replaceWith( $('<input type="checkbox" />'). prop("value", oldInput.prop("value")). prop("name", oldInput.prop("name")). prop("id", oldInput.prop("id")). prop('checked',true) ) }else{ $(value).replaceWith( $('<input type="checkbox" />'). prop("value", oldInput.prop("value")). prop("name", oldInput.prop("name")). prop("id", oldInput.prop("id")) ) } }); // limit selection checkboxes $('input[name="' + id + '"]').change(function(){ var max= 2; if( $('input[name="' + id + '"]:checked').length == max ){ $('input[name="' + id + '"]').prop('disabled', 'disabled'); $('input[name="' + id + '"]:checked').removeProp('disabled'); }else{ $('input[name="' + id + '"]').removeProp('disabled'); } }); } 的特定子元素,您可以这样做。

&#13;
&#13;
<a href="/portal/product/62157993" id="ember1708" class="product-items__item ember-view">
&#13;
var promote = document.querySelector('.product-items__item  .product-item__promote');


promote.addEventListener('click', function(e){
	e.preventDefault();
	console.log('prmotion ending');
});
&#13;
&#13;
&#13;

这样可以确保您只选择推广<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <a href="/portal/product/62157993" id="ember1708" class="product-items__item ember-view"> <div id="ember1709" class="product-item product-view-item ember-view"> <div class="product-item__info"> <div class="product-item__name"> <span data-ember-action="" data-ember-action-1712="1712">Pashmina Denim</span> </div> <div class="product-item__promote"> <div id="ember1713" class="product-boost-button ember-view"> <div id="ember1714" class="shopee-button shopee-button--inactive shopee-button--medium ember-view">Promote </div> </div> </div> </div> </div> </a> </body>类的孩子。