e.target.className包含某个class判断
时间: 2024-04-08 19:30:03 浏览: 117
您可以使用JavaScript中的`classList`属性来判断元素的`className`是否包含某个特定的class。具体的代码示例如下:
```javascript
if (e.target.classList.contains('your-class-name')) {
// 包含指定的class
console.log('包含该class');
} else {
// 不包含指定的class
console.log('不包含该class');
}
```
请将上述代码中的`your-class-name`替换为您要判断的具体class名称。这样,当事件触发时,就会根据`e.target`元素的`className`来判断是否包含指定的class。
相关问题
优化这段代码 let currentDropTarget: any; let transferredImage: HTMLElement | null; function dragTable(e: DragEvent) { if (!e || !e.dataTransfer) return; currentDropTarget = e.target; const field: string | number | undefined = (e.target as HTMLElement)?.id; const column = columnAll.value.find((c) => c.field == field)!; const showedElement = generateDragImage(column?.name); e.dataTransfer?.setDragImage(showedElement || document.body, 0, 0); e.dataTransfer.setData("text/plain", field); } function generateDragImage(name: string) { transferredImage = document.createElement("div"); document.body.appendChild(transferredImage); transferredImage.className = "table-header-transferred-image"; transferredImage.innerText = name; return transferredImage; } const changeBorder = throttle((target) => { if ( (target.draggable || target.classList.contains("s2-index-header")) && currentDropTarget && currentDropTarget != target ) { changeClassName(false); currentDropTarget = target; changeClassName(true); } }, 100); function allowDrop(e: MouseEvent) { e.preventDefault(); const target = e.target as HTMLElement; if (target) changeBorder( target.draggable || target.classList.contains("s2-index-header") ? target : target.parentNode ); } function dropTable(e: DragEvent) { e.preventDefault(); if (!e) return; const dragField = e.dataTransfer?.getData("text/plain") || ""; const dropField = (e.target as HTMLElement)?.id; if (dragField && dropField && dragField != dropField) { changeColumnOrder(dragField, dropField); } } function onDragend() { changeClassName(false); currentDropTarget = undefined; transferredImage && document.body.removeChild(transferredImage); } function changeClassName(isAdded: boolean) { if (isAdded) currentDropTarget.classList.add("s2-self-defined-dom-border"); else currentDropTarget.classList.remove("s2-self-defined-dom-border"); } function changeColumnOrder(dragField: string, dropField: string) { const dragIndex = columnAll.value.findIndex( (item) => item.field == dragField ); const dragColumn = columnAll.value.splice(dragIndex, 1)[0]; const dropIndex = dropField == "-1" ? -1 : columnAll.value.findIndex((item) => item.field == dropField); columnAll.value.splice(dropIndex + 1, 0, dragColumn); dataManager.updateDataConfig({ meta: columnAll.value }); tableManager?.render(dataManager.getFormattedData()); onColumnShowChange(); }
这段代码可以进行如下优化:
1. 使用类型定义来提高代码可读性和类型安全性;
2. 使用解构赋值来简化代码;
3. 函数名可以更具描述性;
4. 使用可选链操作符来简化代码。
优化后的代码如下:
```typescript
type Column = {
field: string | number | undefined;
name: string;
};
let currentDropTarget: HTMLElement | undefined;
let transferredImage: HTMLElement | | undefined;
function handleDragStart(e: DragEvent): void {
const { dataTransfer, target } = e;
if (!dataTransfer) return;
const field = (target as HTMLElement)?.id;
const column = columnAll.value.find((c) => c.field === field)!;
const showedElement = generateDragImage(column.name);
dataTransfer.setDragImage(showedElement || document.body, 0, 0);
dataTransfer.setData("text/plain", field);
currentDropTarget = target as HTMLElement;
}
function generateDragImage(name: string): HTMLElement {
transferredImage = document.createElement("div");
document.body.appendChild(transferredImage);
transferredImage.className = "table-header-transferred-image";
transferredImage.innerText = name;
return transferredImage;
}
const changeBorder = throttle((target: HTMLElement): void => {
if (
(target.draggable || target.classList.contains("s2-index-header")) &&
currentDropTarget &&
currentDropTarget !== target
) {
changeClassName(false);
currentDropTarget = target;
changeClassName(true);
}
}, 100);
function handleDragOver(e: MouseEvent): void {
e.preventDefault();
const target = e.target as HTMLElement;
if (!target) return;
const node = target.draggable
? target
: target.closest(".s2-index-header");
if (node) changeBorder(node);
}
function handleDrop(e: DragEvent): void {
e.preventDefault();
const dragField = e.dataTransfer?.getData("text/plain") || "";
const dropField = (e.target as HTMLElement)?.id || "";
if (dragField && dropField && dragField !== dropField) {
changeColumnOrder(dragField, dropField);
}
}
function handleDragEnd(): void {
changeClassName(false);
currentDropTarget = undefined;
transferredImage && document.body.removeChild(transferredImage);
}
function changeClassName(isAdded: boolean): void {
if (!currentDropTarget) return;
const className = "s2-self-defined-dom-border";
isAdded ? currentDropTarget.classList.add(className) : currentDropTarget.classList.remove(className);
}
function changeColumnOrder(dragField: string, dropField: string): void {
const dragIndex = columnAll.value.findIndex((item) => item.field === dragField);
const dragColumn = columnAll.value.splice(dragIndex, 1)[0];
const dropIndex = dropField === "-1" ? -1 : columnAll.value.findIndex((item) => item.field === dropField);
columnAll.value.splice(dropIndex + 1, 0, dragColumn);
dataManager.updateDataConfig({ meta: columnAll.value });
tableManager?.render(dataManager.getFormattedData());
onColumnShowChange();
}
```
react 点击图标报错uncaught TypeError: a.target.className.indexOf is not a function
这个错误通常是因为你的代码中使用了 `indexOf` 方法,但是该方法只能作用于字符串,而你的 `a.target.className` 可能不是一个字符串类型。
你可以尝试在使用 `indexOf` 之前,先将 `a.target.className` 转换成字符串类型,例如:
```
a.target.className.toString().indexOf(...)
```
或者使用其他方法来判断类名是否存在,例如:
```
a.target.classList.contains(...)
```
如果你提供更多的代码或者错误信息,我可以更准确地帮助你调试问题。
阅读全文