专注于页面无法正常工作 [英] Focus on page not working

查看:73
本文介绍了专注于页面无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果存在CSS类,我将尝试着重于页面中的特定ID.但是我拥有的代码不起作用,我不确定为什么吗?

I am trying to set focus on a certain ID in the page if a CSS class is present. However the code that I have doesn't work and I am not certain why?

$(document).ready(function() {
    if ($('body').hasClass('message')) {
        $('#maincontent').focus();
    }
});

HTML: 页面中用于设置焦点的锚点:

HTML: The Anchor in the page to set focus:

<a name="maincontent" tabindex="-1" id="maincontent"/>

如果该div存在于页面中:

If this div is present in page:

<div class="message success"><p><span class="glyphicon glyphicon-ok-sign" aria-hidden="true"></span> Your submission has been received.</p></div>

推荐答案

将其更改为:

$(document).ready(function() {
    if ($('body .message').length) {
        $('#maincontent').focus();
    }
});

正文在HTML中没有类消息.

The body doesn't have class message in your HTML.

这篇关于专注于页面无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆