gpt4 book ai didi

JavaScript:单击事件上对象的值

转载 作者:行者123 更新时间:2023-11-30 18:20:02 28 4
gpt4 key购买 nike

我正在编写一个 codecademy.com 类(class),教我如何使用 jQuery 构建条形图。最后一组指令是将点击事件添加到 $bar 以显示它的值。解释

Anonymous functions go like this: function(arg) { code }. In this case, a .click handler will have e, the click event, as its argument, so your implementation should have function(e) { alert code } within the .click().

这是我应该修改的函数

// Exercise 5
function addBarClickEvent($bar,value) {
// add a click event to the bar that
// pops up an alert with the bars value
$bar.click(
// your code goes here!

}
);
}

我尝试了下面显示的两种解决方案,但均无效。不幸的是,该类(class)没有提供解决方案。谁能帮忙。

// Exercise 5
function addBarClickEvent($bar,value) {
// add a click event to the bar that
// pops up an alert with the bars value
$bar.click(
// your code goes here!
alert($bar.value) //my attempt
alert(e.value) //my second attempt
}
);
}

我收到此错误消息

Make sure to define the argument to .click as an anonymous function that displays the alert.

最佳答案

假设你要显示的值是绑定(bind)时传入的值

function addBarClickEvent($bar,value) {
// add a click event to the bar that
// pops up an alert with the bars value
$bar.click(function(){
alert(value);
});
}

如果不是——

$bar.click(function(){
// get value somehow
//$(this).data('some-value');
});

关于JavaScript:单击事件上对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12287172/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com