how to make div click-able?

前端 未结 9 1107
春和景丽
春和景丽 2020-12-08 07:15
shanghaimale

For div like above,when mouse on,it should become cursor:poin

相关标签:
9条回答
  • 2020-12-08 07:35

    <div style="cursor: pointer;" onclick="theFunction()">

    is the simplest thing that works.

    Of course in the final solution you should separate the markup from styling (css) and behavior (javascript) - read on it on a list apart for good practices on not just solving this particular problem but in markup design in general.

    0 讨论(0)
  • 2020-12-08 07:36

    The simplest of them all:

    <div onclick="location.href='where.you.want.to.go'" style="cursor:pointer"></div>
    
    0 讨论(0)
  • 2020-12-08 07:39

    I suggest to use jQuery:

    $('#mydiv')
      .css('cursor', 'pointer')
      .click(
        function(){
         alert('Click event is fired');
        }
      )
      .hover(
        function(){
          $(this).css('background', '#ff00ff');
        },
        function(){
          $(this).css('background', '');
        }
      );
    
    0 讨论(0)
  • 2020-12-08 07:39

    add the onclick attribute

    <div onclick="myFunction( event );"><span>shanghai</span><span>male</span></div>
    

    To get the cursor to change use css's cursor rule.

    div[onclick] {
      cursor: pointer;
    }
    

    The selector uses an attribute selector which does not work in some versions of IE. If you want to support those versions, add a class to your div.

    0 讨论(0)
  • 2020-12-08 07:40
    <div style="cursor: pointer;" onclick="theFunction()" onmouseover="this.style.background='red'" onmouseout="this.style.background=''" ><span>shanghai</span><span>male</span></div>
    

    This will change the background color as well

    0 讨论(0)
  • 2020-12-08 07:41

    Give it an ID like "something", then:

    var something = document.getElementById('something');
    
    something.style.cursor = 'pointer';
    something.onclick = function() {
        // do something...
    };
    

    Changing the background color (as per your updated question):

    something.onmouseover = function() {
        this.style.backgroundColor = 'red';
    };
    something.onmouseout = function() {
        this.style.backgroundColor = '';
    };
    
    0 讨论(0)
提交回复
热议问题