* 获取增强的标签信息
(element)
| 443 | * 获取增强的标签信息 |
| 444 | */ |
| 445 | function getEnhancedLabel(element) { |
| 446 | let labelText = ''; |
| 447 | let labelSource = ''; |
| 448 | const id = element.id; |
| 449 | |
| 450 | // 1. 查找 <label for="id"> |
| 451 | if (id) { |
| 452 | const label = document.querySelector(`label[for="${id}"]`); |
| 453 | if (label) { |
| 454 | labelText = label.innerText.trim(); |
| 455 | labelSource = 'label-for'; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // 2. 查找父级 <label> |
| 460 | if (!labelText) { |
| 461 | const parentLabel = element.closest('label'); |
| 462 | if (parentLabel) { |
| 463 | labelText = parentLabel.innerText.trim(); |
| 464 | labelSource = 'parent-label'; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // 3. aria-label |
| 469 | if (!labelText) { |
| 470 | const ariaLabel = element.getAttribute('aria-label'); |
| 471 | if (ariaLabel) { |
| 472 | labelText = ariaLabel; |
| 473 | labelSource = 'aria-label'; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // 4. aria-labelledby |
| 478 | if (!labelText) { |
| 479 | const labelledBy = element.getAttribute('aria-labelledby'); |
| 480 | if (labelledBy) { |
| 481 | const labelEl = document.getElementById(labelledBy); |
| 482 | if (labelEl) { |
| 483 | labelText = labelEl.innerText.trim(); |
| 484 | labelSource = 'aria-labelledby'; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | // 5. aria-describedby (作为补充上下文) |
| 490 | if (!labelText) { |
| 491 | const describedBy = element.getAttribute('aria-describedby'); |
| 492 | if (describedBy) { |
| 493 | const descEl = document.getElementById(describedBy); |
| 494 | if (descEl) { |
| 495 | labelText = descEl.innerText.trim(); |
| 496 | labelSource = 'aria-describedby'; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // 6. title 属性 |
| 502 | if (!labelText) { |