如何正确使用href=“#someID”?

发布于 2024-12-03 13:51:58 字数 356 浏览 6 评论 0原文

基本上,我想要一个链接列表,其中每个链接将显示一个 div。我已经设置好了一切,以便可以正常工作,但我只是不知道哪些事件被触发以及以什么顺序被触发。我想添加功能,以便当显示这些 div 之一时,运行 javascript。但到目前为止,使用如下代码:

在实际显示 div 之前调用 some_script() 。这对我来说毫无用处,因为我试图在显示 div 后让某个输入框成为焦点。

谁能告诉我哪些 jquery 事件被触发,或者我可以使用 jquery 事件,以便我可以在 div 显示给用户后运行 some_script() ?

Basically, I want to have a list of links, where each link will show a div. I have everything set up so that this works, but I just have no idea as to which events are getting fired, and in what order. I want to add functionality so that when one of these divs is shown, javascript is run. But so far using code such as this:

<a href="#div1" onclick="some_script()" />

calls the some_script() before it actually shows the div. Which is useless to me, since I am trying to get a certain input box to be focused, after showing the div.

Can anyone tell me which jquery events get fired, or and jquery events that I can use so that I can run some_script() after the div is shown to the user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2024-12-10 13:51:58

您可以使用类似的内容

$(document).ready(function(){
    $('a').click(function(){ //attaches click event to all 'a' elements on page
        var div = $(this).attr("href").substr(1); //gets the div
        $(div).show(); //show the div
    });
});

此链接应该可以帮助您开始使用 jQuery。 http://docs.jquery.com/Main_Page

You can use something like this

$(document).ready(function(){
    $('a').click(function(){ //attaches click event to all 'a' elements on page
        var div = $(this).attr("href").substr(1); //gets the div
        $(div).show(); //show the div
    });
});

This link should get you started with jQuery. http://docs.jquery.com/Main_Page

原野 2024-12-10 13:51:58

如果我正确理解您的要求:

HTML

<p><a href="#div_one" class="trigger">1st div</a></p>
<p><a href="#div_two" class="trigger">2nd div</a></p>
<div id="div_one">
    Some content
    <input type="text" name="input1" class="input">
</div>
<div id="div_two">
    Some more content
     <input type="text" name="input2" class="input">
</div>

CSS

div {
    border: 2px solid green;
    background-color: lightgreen;
    color: black;
    display: none;
    margin: 10px;
    padding: 5px;
}

JavaScript

$('.trigger').click(function(e) {
    e.preventDefault();
    var target_div = $(this).attr('href');
    $(target_div).toggle();
    $(target_div).find('.input').focus();
});

jsFiddle 演示:http://jsfiddle.net/byuPL/

If I understood your requirements correctly:

HTML

<p><a href="#div_one" class="trigger">1st div</a></p>
<p><a href="#div_two" class="trigger">2nd div</a></p>
<div id="div_one">
    Some content
    <input type="text" name="input1" class="input">
</div>
<div id="div_two">
    Some more content
     <input type="text" name="input2" class="input">
</div>

CSS

div {
    border: 2px solid green;
    background-color: lightgreen;
    color: black;
    display: none;
    margin: 10px;
    padding: 5px;
}

JavaScript

$('.trigger').click(function(e) {
    e.preventDefault();
    var target_div = $(this).attr('href');
    $(target_div).toggle();
    $(target_div).find('.input').focus();
});

jsFiddle demo: http://jsfiddle.net/byuPL/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文