使用jquery document.ready() on <事件

Using jquery document.ready() on <img onload> event

本文关键字:事件 on jquery document ready 使用      更新时间:2023-09-26

我有以下需要:我想调用一个js每次图像加载到页面上,但是我还需要在js运行之前完成加载页面的DOM。

我写了下面的"假"例子,我想知道它是否可以被认为是正确的。它似乎可以工作,但是,因为jquery说:".ready()方法通常与属性",我希望有人告诉我:"是的,你可以做到"。

非常感谢你的帮助。尼科

的例子:

<html>
<head>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    function mySetVal() {
       $(document).ready(function () {
         $("#status").val("Refreshing");
       });
    }
</script>
</head>
<body>

<div style="border:1px solid green">
<img src="target.jpeg" id="cat" onload="mySetVal()"></img>
</div>
<div>
<p>E poi che la sua mano a la mia puose con lieto volto, ond'io mi confortai, mi mise dentro a le segrete cose.</p>
</div>
<table>
<tr>
<th>animale</th><th>pianta</th>
</tr>
<tr>
<td>mucca</td><td>palma</td>
</tr>
</table>
<p>Input: <input type="text" id="status" /></p>
</body>
</html>

是的,你可以这样做,只要jQuery在图像之前加载,因为ready函数将被直接调用,如果DOM已经准备好了,当它被调用

是的,这正是$(document)ready(…)的意思。

看http://docs.jquery.com/Tutorials:Introducing_$%28document%29.ready%28%29

我建议您从img标签中删除内联属性onload。您可以使用以下代码:

$(document).ready(function()
{
    $("#cat").load(function()
    {
        $("#status").val("Refreshing");
    });
});