如何调用jQuery函数onclick?

mil*_*ano 2 html javascript jquery

当我点击提交按钮时,我正在使用此代码将页面的当前网址添加到div中.我没有得到如何调用函数onclick.这该怎么做?

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>localhost</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script type="text/javascript">
            $(function () {
                var url = $(location).attr('href');
                $('#spn_url').html('<strong>' + url + '</strong>');
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div id="spn_url"></div>
            <input type="submit" value="submit" name="submit">
        </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

vin*_*ini 8

请看http://jsfiddle.net/2dJAN/59/

$("#submit").click(function () {
var url = $(location).attr('href');
$('#spn_url').html('<strong>' + url + '</strong>');
});
Run Code Online (Sandbox Code Playgroud)


Tus*_*har 6

试试这个 ..

HTML

<input type="submit" value="submit" name="submit" id="submit">
Run Code Online (Sandbox Code Playgroud)

jQuery的

$(document).ready(function () {
    $('#submit').click(function () {
        var url = $(location).attr('href');
        $('#spn_url').html('<strong>' + url + '</strong>');
    });
});
Run Code Online (Sandbox Code Playgroud)

  • **+ 1**`$(位置).attr('href');`可能是`location.href`,不需要传递给jQuery :) (3认同)