Javascript call problem

1:
<script>
    $(function () {
        var $butObj = $("-sharpbutObj");
        $butObj.click(function () {
            var $uname = $("-sharpuserName");
            var inputName = $uname[0];
            alert(inputName.value);
        });
    })
</script>
2:
    <script>
        var $butObj = $("-sharpbutObj");
        $butObj.click(function () {
            var $uname = $("-sharpuserName");
            var inputName = $uname[0];
            alert(inputName.value);
        });
</script>

<body>
    :
    <input type="text" id="userName">
    <input type="button" id="butObj" value="">
</body>
clickclick
Mar.02,2021

html is executed sequentially before the dom is rendered before your script is executed, so the second one is not bound with click time. Jq's dollar method is actually very similar to onload, which is performed after dom rendering, so the first way is to find the dom node and bind the click event


isn't it the same for you? is it my poor eyesight?

Menu