请教各位JS JQUERY高手一个简单问题 急急急~~

NickHuang1127 2013-05-20 11:59:52
页面有多个div 切每个div套有超链接 如:
<div class="a"><a href="a.html"></a></div>
<div class="a"><a href="b.html"></a></div>
<div class="a"><a href="c.html"></a></div>
<div class="a"><a href="d.html"></a></div>
如何让页面加载时默认执行第一个DIV的超链接事件 默认页面加载后就跳到a.html
JS和JQUERY都可以 急急急~
...全文
264 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
NickHuang1127 2013-05-21
  • 打赏
  • 举报
回复
最后解决了 给 a 标签加上 ID或class 然后 location.href = $("#id名").attr("href");或 location.href = $(".class名").attr("href"); 调用点击事件只能是a标签的链接 调用外围的DIV不起作用的 谢谢大家 分都给大家
O15013245O 2013-05-20
  • 打赏
  • 举报
回复

$(document).ready(function () {
    location.href = $(".a").attr("href");
});
NickHuang1127 2013-05-20
  • 打赏
  • 举报
回复
引用 1 楼 imbibi 的回复:
$(document).ready(function () { $(".a").click(); }); 自己用JQUERY这样写 但没有跳转
而且默认还是最后一个DIV被点击了 不是第一个DIV
NickHuang1127 2013-05-20
  • 打赏
  • 举报
回复
$(document).ready(function () { $(".a").click(); }); 自己用JQUERY这样写 但没有跳转
kinghome 2013-05-20
  • 打赏
  • 举报
回复
曾见过有人提过这个问题... ... 我给了他这个方法... ...却... ... 不结贴....

function e(event){
    var evt = event || window.event;
    var target = evt.target || evt.srcElement;
    window.location = target.id+".html";
}
/*
   @linkId : 不需要跳转的 A 元素 ID
*/
function setLinks(linkId){
    var menus = document.getElementById("menus").getElementsByTagName("a");
    for(var i=0; i< menus.length; i++){
        if(menus[i].id.toLowerCase() != linkId.toLowerCase()){
             if (window.attachEvent){
                 menus[i].attachEvent('click',e)
             }else if(window.addEventListener){
                 menus[i].addEventListener('click',e,false)
            }
        }   
    }
     
}
window.onload = function(){
    setLinks('a1');
}

<div id="menus">
    <a ID=a1 href="#"> 菜单1</a>
    <a ID=a2 href="#"> 菜单2</a>
    <a ID=a3 href="#"> 菜单3</a>
    <a ID=a4 href="#"> 菜单4</a>
</div>
cupsBing 2013-05-20
  • 打赏
  • 举报
回复
$(document).ready(function(){location.href=$("div:first a").attr("href")});
  • 打赏
  • 举报
回复
很简单啊 $('div.a').first().find('a').trigger('click');
呵呵我来啦 2013-05-20
  • 打赏
  • 举报
回复
8L +1 class a中选取第一个 也可以这样试试

$(document).ready(function () {
      window.location.href=$(".a:eq(0) a").attr("href");
});
NANU-NANA 2013-05-20
  • 打赏
  • 举报
回复
引用 6 楼 imbibi 的回复:
注意审题
你不就是要问怎么跳转么? 哦,是不是因为我没写 本例中,文本 "Page is loaded" 会被显示在状态栏中: <html> <head> <script type="text/javascript"> function load() { window.location.href = "a.htm"; } </script> </head> <body onload="load()"> </body> </html>
天际的海浪 2013-05-20
  • 打赏
  • 举报
回复
这个是正确的,被楼上绕进去了

$(document).ready(function () {
    location.href = $(".a:eq(0) a")[0].href;
});
天际的海浪 2013-05-20
  • 打赏
  • 举报
回复
$(document).ready(function () { location.href = $(".a")[0].href; });
NickHuang1127 2013-05-20
  • 打赏
  • 举报
回复
引用 5 楼 u010087908 的回复:
window.location.href = "a.htm";
注意审题
NANU-NANA 2013-05-20
  • 打赏
  • 举报
回复
window.location.href = "a.htm";
NickHuang1127 2013-05-20
  • 打赏
  • 举报
回复
引用 3 楼 O15013245O 的回复:

$(document).ready(function () {
    location.href = $(".a").attr("href");
});
你这个直接页面报错 - -

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧