单击div和带JS的按钮后重定向用户

时间:2016-07-13 20:55:45

标签: javascript jquery

我在点击div和下一个按钮后使用此代码重定向用户, 但它不适合我:(

这是我的代码



$("a.xxx").click(function() {
  $(this).toggleClass("yyy").toggleClass("xxx");
});
$(".resellc").click(function() {
var rid = document.getElementsByTagName('yyy').id;
location.href = rid;
});

<a id="3" class="xxx"><div style="color: black; padding: 10px; border-bottom: 0.1px solid;">Apple watch</div></a><a id="18" class="xxx"><div style="color: black; padding: 10px; border-bottom: 0.1px solid;">Apple Watch</div></a><a id="21" class="xxx"><div style="color: black; padding: 10px; border-bottom: 0.1px solid;">BMW Bike</div></a><a id="30" class="xxx"><div style="color: black; padding: 10px; border-bottom: 0.1px solid;">Women lingerie</div></a>
<button class="resellc">next</button>
&#13;
&#13;
&#13;

由于

1 个答案:

答案 0 :(得分:1)

您可以使用:

$("a.xxx").click(function(e) {
    e.preventDefault();
    var rid = this.id;
    $(this).toggleClass("yyy").toggleClass("xxx");
    $(".resellc").off('click').one('click', function() {
        location.href = rid;
        console.log(rid);
    });
});

$("a.xxx").click(function(e) {
    e.preventDefault();
    var rid = this.id;
    $(this).toggleClass("yyy").toggleClass("xxx");
    $(".resellc").off('click').one('click', function() {
        location.href = rid;
        console.log(rid);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="3" class="xxx"><div style="color: black; padding: 10px; border-bottom: 0.1px solid;">Apple watch</div></a><a id="18" class="xxx"><div style="color: black; padding: 10px; border-bottom: 0.1px solid;">Apple Watch</div></a><a id="21" class="xxx"><div style="color: black; padding: 10px; border-bottom: 0.1px solid;">BMW Bike</div></a><a id="30" class="xxx"><div style="color: black; padding: 10px; border-bottom: 0.1px solid;">Women lingerie</div></a>
<button class="resellc">next</button>