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

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

“d”參數在 .on 事件中返回未定義

“d”參數在 .on 事件中返回未定義

子衿沉夜 2023-05-25 16:06:21
d我遇到D3.js 中的參數返回的問題undefined。這是我現在的代碼const buttons = d3.select("body")  .data(countries)  .enter()  .append("button")  .text((d) => { return d.location; })              // returns name location  .attr("value", (d) => { return d.location; })     // returns name location  .on("click", (d) => { console.log(d.location) }); // returns undefined在按鈕上,我看到了正確的文本和位置值,因此它在那里正常工作,但在我的點擊事件中,我想更改一個具有特定國家/地區名稱的變量,但它返回未定義。
查看完整描述

1 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

這是因為“點擊”事件event首先給你參數,然后是datum

當在選定元素上調度指定事件時,將為該元素評估指定的偵聽器,傳遞當前事件(event)和當前數據(d),并將其作為當前 DOM 元素(event.currentTarget)。聽眾總是看到他們元素的最新數據。

我想你正在尋找這個:

const countries = [

? {

? ? location: 'USA',

? },

? {

? ? location: 'Canada',

? }

];


const buttons = d3.select("body")

? .selectAll('button')

? .data(countries)

? .enter()

? .append("button")

? .text((d) => { return d.location; })? ? ? ? ? ? ? // returns name location

? .attr("value", (d) => { return d.location; })? ? ?// returns name location

? .on("click", (e, d) => { console.log(d.location) }); // returns name location


查看完整回答
反對 回復 2023-05-25
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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