基于div a href链接重定向

时间:2013-04-19 17:06:20

标签: jquery redirect

我正在寻找一种基于div的href链接重定向页面的方法。如果页面上只有一个div,则重定向到该div的href链接。

JS

$(function () { 
    if ($('.div').length = 1) { alert('Just one Div'); } 
});

HTML

<div class="div"><div class="title"><a href="../posters/bicycles/default.html">Bicycle</a></div>

2 个答案:

答案 0 :(得分:1)

你可以这样做,返回DIV元素的数量:

if( $("div").length == 1 )
{
    document.location = $("div a").attr("href");
}

答案 1 :(得分:1)

试试这个:

$('.div a').click(function (e) {
    e.preventDefault();
    window.location.href = $(this).attr('href');
});
相关问题