简体   繁体   English

使 <div> , <li> 无<a>标签</a>可点击

[英]Make <div>, <li> clickable without <a> tag

I want a <div> or <li> tag to be clickable (the pointer should change to hand). 我希望<div><li>标签可以点击(指针应该变为手)。 How can I do it without <a> tag? 没有<a>标签我该怎么办?

Because, when a div is clicked, my javascript loads a text file. 因为,当点击div时,我的javascript会加载一个文本文件。

您应该为手形指针添加style="cursor:pointer" ,并且onclick="window.location='somewhere'"

<div style="cursor:pointer" onclick="window.location='index.html'">My Link</div>
var myel = document.querySelector('#myel'); // your element

myel.addEventListener('click', function() {
  // do stuff
});

In CSS: 在CSS中:

#myel { cursor: pointer }

you can do it a number of ways. 你可以通过多种方式做到这一点。 one way is to assign the pseudo class in your css. 一种方法是在你的CSS中分配伪类。 if the div has a class of div1 then our css would read: 如果div有一个div1类,那么我们的css会读:

.div1:hover{
    cursor:pointer;
}

You can also do it in javascript 你也可以用javascript来做

$('.div1').on("hover", function(){
    $(this).css("cursor", "pointer");
});

Set the css: 设置CSS:

ul li{
  cursor:pointer; /*  <------this will change the cursor to hand cursor*/
}

and jQuery: 和jQuery:

$(function(){
   $('li').click(function(){
      alert('li clicked.');
   });
});
        $("div").hover(function(){
        $(this).css('cursor','pointer')
});

Hope this helps... 希望这可以帮助...

CSS CSS

li:hover {
    cursor: pointer //on hover change the cursor to pointer
}

JQuery JQuery的

$(document).ready(function() { 
    $("#click_me").bind("click", function(){ //bind a click event on an element with id = click_me
        $.ajax({ 
            url: "./file.txt", //note: file must be in the same domain as the current script http://en.wikipedia.org/wiki/Same_origin_policy
            dataType: "text",
            success: function(data) { //if ajax request is successful
                $("#result_container").html(data); //append the resulting data into a div with id = result_container
        }
    });
});

}); });

Cheers :) 干杯:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM