English 中文(简体)
在点击图像时如何操作javascript功能?
原标题:how to run javascript function when click on image?

i 有:

<ul class="cont_ul4">

<li class="cont_li">
<div class="cont_picture">
<a href=""><img src="1.jpg" width="150" height="225" ></a>
</div>
</li>

<li class="cont_li">
<div class="cont_picture">
<a href=""><img src="2.jpg" width="150" height="225" ></a>
</div>
</li>

</ul>

以及这一职能:

function ajax_request() {
$( #placeholder ).html( <p><img class="loader" src="/loader.gif"></p> );
$( #placeholder ).load("/test.php?id=1234556");
}

what i need is when i click on the image to trigger this function.

有了投入,可以做到:

<input type="button" onclick="ajax_request()" value="Click Me!" />

any ideas? Thanks

最佳回答

image:

<img id="myImage" src="1.jpg" width="150" height="225" style="cursor:pointer">

然后是 Java字:

$("#myImage").click(ajax_request);

或者,如果你想要为所有图像发挥同样的作用:

$("img").click(ajax_request);

然而,最好把图像贴出一个CSS级,然后加以利用。 这样,你就可以把点击行动限制在具体图像上:

<img class="link-image" src="1.jpg" width="150" height="225">
...
<img class="link-image" src="2.jpg" width="150" height="225">

然后:

$(".link-image").click(ajax_request);
问题回答

你们是否喜欢这样的东西?

$(function(){
    $("img").click(function(){
        ajax_request();
    });
});

Try this:

JS:

function ajax_request() {
   $( #placeholder ).html( <p><img class="loader" src="/loader.gif"></p> );
   $( #placeholder ).load("/test.php?id=1234556");
}


$( #run_ajax ).click(ajax_request);

传真:

<input type="button" id= run_ajax  value="Click Me!" />




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签