views:

2395

answers:

7
<div><span>shanghai</span><span>male</span></div>

For div like above,when mouse on,it should become cursor:pointer,and when clicked,fire a

javascript function,how to do that job?

EDIT: and how to change the background color of div when mouse is on?

EDIT AGAIN:how to make the first span's width=120px?Seems not working in firefox

A: 

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.

apphacker
Obtrusive much!?
J-P
Why's this obtrusive? It does what the OP asked for.
JonoW
+1  A: 

<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.

macbirdie
Obtrusive much!?
J-P
and how to change the background color of div when mouse is on?
Shore
+1  A: 

If this div is a function i sugest use cursor:hand in your style like style="cursor:hand" and can use onclick function.

like this

<div onclick="myfunction()" style="cursor:hand"></div>

but i sugest you use a js framework like jquery or extjs

pho3nix
Why should he/she use an entire framework for something so simple!?!?
J-P
cursor:hand is no good on browsers other than IE. cursor:pointer is the cross browser way of doing "the hand"
Dan F
Obtrusive much!?
J-P
A: 

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 = '';
};
J-P
Remember to do this inside the window.onload-event.
alexn
Or, place the script somewhere beneath the element being targeted.
J-P
btw,how to make the first span's width:120px?
Shore
span doesn't take a width because it's an inline element (it's as wide as it's content), can you make it a div instead?
henrikpp
why inline element can't take a width?
Shore
i changed that div to a table,and span to td,this time works.is td an inine element or not?
Shore
Don't use a table unless you're showing tabular data. You can give a span a width, you just need to set it to display:block; first.
J-P
no,i need it to display as inline.
Shore
Then you can´t assign a width, it´s that simple.
Daniel
but i can assign a width to td,aren't they both inline elements?
Shore
td is block level elements:http://www.ahuka.com/htmllevel1/blocklevel.html
Wondering
http://htmlhelp.com/reference/html40/block.html ...can also b helpful for u.
Wondering
@Wondering,so all TDs in a TR use float to display like inline?
Shore
+1  A: 

As you updated your question, here's an obtrustive example:

window.onload = function()
{
    var div = document.getElementById("mydiv");

    div.style.cursor = 'pointer';
    div.onmouseover = function()
    {
        div.style.background = "#ff00ff";
    };
}
alexn
A: 
<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

Rakesh Juyal
+1  A: 

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', '');
    }
  );
Liu Peng
As has been posted in a comment above; why use a framework for such a simple thing?
Jesper Karsrud
but still +1 for nicely written code
Rakesh Juyal
If there are many simple things in your project, I think a good framework can save a great deal of time.If you want a javascript framework, jQuery is a best option :)
Liu Peng