<div *ngIf="condition; then thenBlock else elseBlock"></div> <ng-template #thenBlock>Content to render when condition is true.</ng-template> <ng-template #elseBlock>Content to render when condition is false.</ng-template>
const table = document.getElementById("my-table"); table.addEventListener("click", (e) => { // Only handle click on td element. if (e.target.tagName.toLowerCase() === "td") { console.log( `You clicked on td element with value ${e.target.innerHTML}` ); } });
为啥突然想到这个呢?
因为最近在做一个drag and drop的app,需要在拖拽的时候显示preview(被拖拽元素跟着鼠标走),需要一个操作就是克隆被拖拽的元素,而cloneNode这个方法是无法克隆事件的(只能克隆inline事件,无法克隆通过属性或者event listener添加的事件),而如果使用的是事件代理模式,则不存在这个问题。
Posted onEdited onInjavascript Symbols count in article: 2.6kReading time ≈9 mins.
什么是跨域
相信很多做前端开发的同学都在浏览器控制台遇到过如下错误。
1
Access to XMLHttpRequest at 'http://localhost:3000/api/xxx' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Posted onEdited onInjavascript
,
array Symbols count in article: 358Reading time ≈1 mins.
What is array-like object?
Array-like object is an object that has a length property and indexed elements. For example, arguments is an array-like object.
Has indexed access to the elements and a non-negative length property to know the number of elements in it. These are the only similarities it has with an array. Doesn’t have any of the Array methods like push, pop, join, map, etc.
1 2 3 4 5 6 7 8 9
const arrayLike = { 0: 'a', 1: 'b', 2: 'c', length: 3, }; console.log(arrayLike[1]); // 'b' console.log(arrayLike.length); // 3 console.log(arrayLike.push); // Uncaught TypeError: arrayLike.push is not a function
What is the difference between array and array-like object?
类型
length属性
索引访问
有Array.prototype方法
Array
✔️
✔️
✔️
Array-like object
✔️
✔️
❌
Which type in JavaScript is array-like object?
There are many types in JavaScript are array-like object, including:
slice return a shallow copy of a portion of an array into a new array object selected from begin to end (not included). The original array will not be modified.