Help us understand the problem. What is going on with this article?

jQuery ポップアップ

More than 3 years have passed since last update.

href使わずにpopup

jsめも
javascript:void(0) は値を何も返さないが、hoverでリンク先表示されるとキモい
これでhref使わずにできる↓
data-target=""は-の後に任意の値、.data('target') で値をとる
(業務においては新しくコーディングルールをつくらないといけないので)
汎用性を考えると、dataを使うのは微妙
状況に応じて使う

html
<p class="table-bottom-text"><a class="popup" href="javascript:void(0)" data-target="#construction-popup">> 詳細をみる</a></p>
js
$(function () {
    $('.popup').click(function(){
        var target = $($(this).data('target'));
        target.fadeIn();
        target.find('.close,.bg').click(function(){
            target.fadeOut();
        });
        //return false;
    });
});

もとのjs、汎用性を考えた実装

js
$(function () {
    $('.popup').click(function(){
        var target = $($(this).attr('href'));
    // これはボタンおして、ページ遷移する前に実行される
        target.fadeIn();
        target.find('.close,.bg').click(function(){
            target.fadeOut();
        });
        return false;
        // これでハッシュつけないようにできる、
        // この後つづけて$('.popup').click(function(){ のイベント設定するとリターンフォルスで実行されない
    });
});
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした