未捕获的类型错误:不能设置属性'src'当想要切换图像时为null

Uncaught TypeError: Cannot set property 'src' of null when want to switch the image

本文关键字:图像 null src 错误 类型 不能 设置 属性      更新时间:2023-09-26

我一直得到未捕获的TypeError:当我想切换图像时,不能设置null错误的属性'src'。

我使用这两个文件的jquery

<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.2/jquery-ui.min.js"></script>

javascript代码

function correctcheck() {
    var value = document.getElementById("codes").value;
    var img = document.getElementById("gameplay");
    if (value == "$player //Declare player as an object'n'n$player = 'Artix' //Set player to Artix'n'necho = 'I am'.$player.'I got the key' // Echo the message'n'n") {
        img.src="correct.gif"; //error is here
        window.alert("Congratulation. You passed forest level.");

   return false;
    } else {
        window.alert("Your Drag and drop code is incorrect..View tips to get correct answer.");
    }
}
这是我的img src的html代码
    <div class="featured">
        <div class="wrap-featured grid">
            <div class="slider">
                <img  src="Artix.png" alt = "GameBanner" name="gameplay" id "gameplay"/>
                <textarea name="codes" id="codes" class="txtDropTarget" cols="80" rows="10"></textarea>
                <a class="button" onClick="correctcheck()"  >Run</a>
            </div>
        </div>
    </div>

你必须在你的img标签中插入"="元素,像这样:

<img  src="Artix.png" alt = "GameBanner" name="gameplay" id="gameplay"/>

必须插入$(document)。在你的代码中准备好和在你的img标签中的"="元素,像这样:

<img  src="Artix.png" alt = "GameBanner" name="gameplay" id="gameplay"/>
function correctcheck() {
       $(document).ready(function(){
           var value = document.getElementById("codes").value;
           var img = document.getElementById("gameplay");
                  if (value == "$player //Declare player as an object'n'n$player = 'Artix' //Set player to Artix'n'necho = 'I am'.$player.'I got the key' // Echo the message'n'n") {
                    img.src="correct.gif"; //error is here
                    window.alert("Congratulation. You passed forest level.");
                    return false;
                } else 
                    {
                   window.alert("Your Drag and drop code is incorrect..View tips to get correct answer.");
                   }}
       )};