我有一个Autocomplete multiple select
元素。当我点击十字删除某些选项时,我想获取这个值。我尝试使用ChipProps = {{onDelete: some function}}
但它不起作用:(我明白了undefined
,而且删除功能根本停止工作。这是我的代码
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
{ title: 'The Dark Knight', year: 2008 },
{ title: '12 Angry Men', year: 1957 },
{ title: "Schindler's List", year: 1993 },
];
<Autocomplete
multiple
id="tags-standard1"
options={top100Films}
getOptionLabel={(option) => option.title}
//ChipProps = {{onDelete: (option) => {console.log(option.title)}}}
renderInput={(params) => …
Run Code Online (Sandbox Code Playgroud) 我是初学者编码器。可能很简单,但我试图找到答案并没有成功。我的问题是为什么对象的属性width
和height
属性都div
返回,undefined
而它们显然都是 100px?
在本主题中解释了如何获取.offsetWidth
属性。但据我所知,它与.width
.
window.onload = function() {
var test = document.getElementById("test");
test.addEventListener("click", select);
function select(e) {
var elementID = e.target.id;
var element = document.getElementById(elementID);
var width = element.width;
console.log(element);
console.log(width);
}
}
Run Code Online (Sandbox Code Playgroud)
div#test {
position: absolute;
height: 100px;
width: 100px;
background-color: black;
}
Run Code Online (Sandbox Code Playgroud)
<div id="test"></div>
Run Code Online (Sandbox Code Playgroud)
我的答案
谢谢大家的回答。他们推动我找到自己的简单解决方案,我希望这对我这样的初学者有所帮助。答案是:div
DOM 对象没有.width
和.height
属性,即使您在 CSS 中分配它们并且它们运行良好。为此,它分别具有.style.width
和.style.height
。但即使您通过 CSS 分配它们,它们也不会出现, element.style
除非您有目的地使用 Java Script。因此,要通过 JS获取元素的 …