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

[ド素人メモ] divタグをリンクにする方法(jQueryをさらりと使います)

More than 5 years have passed since last update.

僕のスキルは、ド素人です。エンジニアではなくディレクターとか営業とかです。

jQueryとか使ったことありません。
(正確には、「ずっと前に一度触ろうとして、すぐ挫折。今だに使ったことはない。」)

最初はCSSだけで実装しようとした。

hoge.html
<div class="wrap">
    <a href="http://foo.com">リンクテキスト</a>
</div>
hoge.css
.wrap {
    height: 300px;
    width: 300px;
    background-color: #ff0000;
    position: relative;
}

.wrap a {
    display: inline-block;
    text-align: center;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

こんな風にaタグを親要素のdivと同じ範囲に広げれば良い。

達人に言われたこと

その方法、あんま使わない方がいいよ。

あ…そうなんですね!分かりました!

教えてもらった方法

hoge.html

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

<div class="wrap" data-url="http://foo.com">
リンクテキスト
</div>

<script>
    jQuery('.wrap').click(function() {
        location.href = jQuery(this).attr('data-url');
    });
</script>

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
ユーザーは見つかりませんでした