亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 函數脫離了范圍?“X 不是函數” “X 是 ”“ ”

Javascript 函數脫離了范圍?“X 不是函數” “X 是 ”“ ”

至尊寶的傳說 2022-08-04 16:53:37
我有一個JS函數,該函數在單擊時調用,該函數傳遞一個字符串,并且第一個按鈕有效,但是所有后續按鈕都給我錯誤action is not a function (In 'action("upvote")', 'action' is "")其中 action 是函數的名稱,upvote 是傳遞的變量。使用檢查元素向我顯示兩個按鈕是相同的,這是它們的外觀<button type="button" onclick="action('upvote')">Like</button>最奇怪的是按鈕在它調用函數之前,并且適用于所有按鈕,而不僅僅是第一個按鈕<button type="button" onclick="fun(84)">Reply</button>我檢查了,我沒有忘記關閉任何div或按鈕我唯一能想到的是,我通過遞歸php函數來回顯這些,我不認為JS函數有范圍,但我不明白為什么第一個按鈕工作而不是后續如果需要更多代碼,請告訴我下面的代碼是我對父 divs 的打印函數 echo "<div class='parent' style='margin-left:".$width."px'>".$x['comment']."    <div class='actions'>       <button  type='button' onclick='fun($ran)'>Reply</button>        <button type='button' onclick='action(\"upvote\")'>Like</button>         <button type='button'>Dislike</button>";    //Reply Like and Dislike are all actions every user gets, here I check which user it is to see if they can see the edit/delete    //Normally I would check for admin rather than id == 2, but there is only 1 admin and he id 2    if(($comment['userid'] == $_SESSION['id']) || $_SESSION['id'] == 2){         echo "<button type='button'>Edit</button>               <button type='button'>Delete</button></div>";//Close of actions div      }     else{        echo"</div>";//Close of actions div      }    $uname = mysqli_fetch_assoc($db->query("SELECT username FROM users WHERE id = ".$comment['userid']." "));    echo"    <div class='info'>    Score: ".$comment['score']." &nbsp; &nbsp; Posted By- ".$uname['username']."&nbsp; &nbsp At-".$x['created']." ";    if($x['edited'] != NULL){       echo"&nbsp; &nbsp; Edited Last-".$x['edited']." </div>";    }    else{       echo"</div>";    }?> </div> //Close of parent div這里仍然是我檢查注釋是否有任何回復注釋的函數的一部分,如果是這樣,我遞歸調用相同的函數,
查看完整描述

1 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

action中的內聯事件處理程序引用包含單擊元素的窗體的屬性。它隱藏全局函數,為函數使用不同的名稱,或者更確切地說,用于附加事件。您可以在代碼段中看到該值。actionactionaddEventListener


function foo(a) {

  console.log(a);

}

<form>

  <button type="button" onclick="foo(action);">

    Click

  </button>

</form>


這背后的原因是,內聯處理程序中的代碼使用(或類似的內部作用機制)限定為事件目標元素,并且當從元素本身找不到給定的變量(實際上的屬性)時,請查找祖先元素,直到找到該屬性。如果未從元素中找到它,則要搜索的最后一個對象是 ,通過這種方式,它可以找到要執行的全局函數,前提是在升級到with (event.target) {...}windowwindow.


查看完整回答
反對 回復 2022-08-04
  • 1 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號