如何在html for Javascript中添加onclick事件-由非程序员编写
这是我的第一篇帖子,我正在寻求帮助。我不是任何类型的程序员,但热衷于理解编程,并建立了一个示例。我试图与一些人分享,他们问这个页面允许鼠标点击吗 我想让它在没有键盘点击事件的情况下工作,例如,如何在html for Javascript中添加onclick事件-由非程序员编写,javascript,html,onclick,Javascript,Html,Onclick,这是我的第一篇帖子,我正在寻求帮助。我不是任何类型的程序员,但热衷于理解编程,并建立了一个示例。我试图与一些人分享,他们问这个页面允许鼠标点击吗 我想让它在没有键盘点击事件的情况下工作,例如,onkeydown=或onkeydup=,这就是现在发生的情况,但通过鼠标点击可以在手机浏览器上工作 如果有人能编写/显示我需要的代码,我将非常感激。我发现HTML需要定义onclick事件,然后做两件事,第一件是speakPrevious,第二件是getNewRandomColor。我认为JavaScri
onkeydown=
或onkeydup=
,这就是现在发生的情况,但通过鼠标点击可以在手机浏览器上工作
如果有人能编写/显示我需要的代码,我将非常感激。我发现HTML需要定义onclick
事件,然后做两件事,第一件是speakPrevious
,第二件是getNewRandomColor
。我认为JavaScript需要定义onclick
元素?但我不确定
`HTML`
<body onkeydown="speakPrevious()">
<body onkeyup="getNewRandomColor()">
`JS`
speakPrevious();
function speakPrevious() {
var synth = window.speechSynthesis;
var utterThis = new SpeechSynthesisUtterance(window.value);
synth.speak(utterThis);
}
getNewRandomColor();
function getNewRandomColor() {
var myArray = ['Red','Green', 'Blue', 'Yellow', ];
var rand = myArray[Math.floor(Math.random() * myArray.length)];
document.getElementsByTagName("body")[0].style.backgroundColor = rand;
var oldRand = rand;
window.value = oldRand;
}
`HTML`
`JS`
speakPrevious();
函数speakPrevious(){
var synth=window.speechSynthesis;
var-utterThis=新的语音合成(window.value);
合成说话(说这个);
}
getNewRandomColor();
函数getNewRandomColor(){
var myArray=['红色','绿色','蓝色','黄色',];
var rand=myArray[Math.floor(Math.random()*myArray.length)];
document.getElementsByTagName(“body”)[0].style.backgroundColor=rand;
var oldRand=rand;
window.value=oldRand;
}
您可以允许用户通过键盘或单击页面(如果使用事件侦听器)与网页进行交互。
在javascript中,您可以添加代码(如下所示),只要单击html body元素,就会触发函数代码。这将添加允许用户单击页面的功能
const page = document.querySelector("body");
page.addEventListener("click", function () {
speakPrevious();
getNewRandomColor();
});
此外,您可能会发现将正文标记事件合并为一个正文标记和两个事件更容易
<body onkeydown="speakPrevious()" onkeyup="getNewRandomColor()">
这将允许用户单击页面以触发功能或使用键盘事件
我的最终文档代码如下所示
<html>
<body onkeydown="speakPrevious()" onkeyup="getNewRandomColor()">
<p>The algorithm will select colors at random from RGBY and you may see the same color several times in a row. This
is expected behavior and NOT a fault.<br>Click inside the color and press any keyboard key to move to the next
color.</p>
</body>
<script>
const page = document.querySelector("body");
page.addEventListener("click", function () {
speakPrevious();
getNewRandomColor();
});
function speakPrevious() {
var synth = window.speechSynthesis;
var utterThis = new SpeechSynthesisUtterance(window.value);
synth.speak(utterThis);
}
function getNewRandomColor() {
var myArray = ['Red', 'Green', 'Blue', 'Yellow', ];
var rand = myArray[Math.floor(Math.random() * myArray.length)];
document.getElementsByTagName("body")[0].style.backgroundColor = rand;
var oldRand = rand;
window.value = oldRand;
}
</script>
</html>
该算法将从RGBY中随机选择颜色,您可能会在一行中多次看到相同的颜色。这
是预期的行为,而不是故障。
单击颜色内部,然后按任意键盘键移动到下一个
颜色
const page=document.querySelector(“正文”);
page.addEventListener(“单击”),函数(){
speakPrevious();
getNewRandomColor();
});
函数speakPrevious(){
var synth=window.speechSynthesis;
var-utterThis=新的语音合成(window.value);
合成说话(说这个);
}
函数getNewRandomColor(){
var myArray=['红色'、'绿色'、'蓝色'、'黄色'、];
var rand=myArray[Math.floor(Math.random()*myArray.length)];
document.getElementsByTagName(“body”)[0].style.backgroundColor=rand;
var oldRand=rand;
window.value=oldRand;
}
谢谢Patadam,该代码已经添加并生效。一个快速跟进的问题当你触摸文本时,代码会起作用,这可以通过触摸屏幕上的任何地方来改变吗?我目前无法复制这个问题,因为我在移动和桌面上的所有尝试,当用户点击页面上的任何地方时,应用程序会起作用。我已经附上了我的答案我的完整的最终HTML和Javascript,所以你可以比较。希望这会有所帮助。谢谢你,帕特亚当,在我的Mac电脑上,它通过键盘改变颜色和说话,但当我使用触摸板时,我必须让光标围绕顶部的文本。谢谢你的努力。我很好奇,想看看它是否可以改变。