至尊寶的傳說(shuō)
2022-08-04 16:53:37
我有一個(gè)JS函數(shù),該函數(shù)在單擊時(shí)調(diào)用,該函數(shù)傳遞一個(gè)字符串,并且第一個(gè)按鈕有效,但是所有后續(xù)按鈕都給我錯(cuò)誤action is not a function (In 'action("upvote")', 'action' is "")其中 action 是函數(shù)的名稱,upvote 是傳遞的變量。使用檢查元素向我顯示兩個(gè)按鈕是相同的,這是它們的外觀<button type="button" onclick="action('upvote')">Like</button>最奇怪的是按鈕在它調(diào)用函數(shù)之前,并且適用于所有按鈕,而不僅僅是第一個(gè)按鈕<button type="button" onclick="fun(84)">Reply</button>我檢查了,我沒有忘記關(guān)閉任何div或按鈕我唯一能想到的是,我通過(guò)遞歸php函數(shù)來(lái)回顯這些,我不認(rèn)為JS函數(shù)有范圍,但我不明白為什么第一個(gè)按鈕工作而不是后續(xù)如果需要更多代碼,請(qǐng)告訴我下面的代碼是我對(duì)父 divs 的打印函數(shù) 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']." Posted By- ".$uname['username']."   At-".$x['created']." "; if($x['edited'] != NULL){ echo" Edited Last-".$x['edited']." </div>"; } else{ echo"</div>"; }?> </div> //Close of parent div這里仍然是我檢查注釋是否有任何回復(fù)注釋的函數(shù)的一部分,如果是這樣,我遞歸調(diào)用相同的函數(shù),
1 回答
PIPIONE
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
action中的內(nèi)聯(lián)事件處理程序引用包含單擊元素的窗體的屬性。它隱藏全局函數(shù),為函數(shù)使用不同的名稱,或者更確切地說(shuō),用于附加事件。您可以在代碼段中看到該值。actionactionaddEventListener
function foo(a) {
console.log(a);
}
<form>
<button type="button" onclick="foo(action);">
Click
</button>
</form>
這背后的原因是,內(nèi)聯(lián)處理程序中的代碼使用(或類似的內(nèi)部作用機(jī)制)限定為事件目標(biāo)元素,并且當(dāng)從元素本身找不到給定的變量(實(shí)際上的屬性)時(shí),請(qǐng)查找祖先元素,直到找到該屬性。如果未從元素中找到它,則要搜索的最后一個(gè)對(duì)象是 ,通過(guò)這種方式,它可以找到要執(zhí)行的全局函數(shù),前提是在升級(jí)到with (event.target) {...}windowwindow.
添加回答
舉報(bào)
0/150
提交
取消
