| 2567 | |
| 2568 | // 移除一个元素(子元素)的attr |
| 2569 | function removeAttrs(elem) { |
| 2570 | var attrs = elem.attributes || []; |
| 2571 | var attrNames = []; |
| 2572 | var exception = ['href', 'target', 'src', 'alt', 'rowspan', 'colspan']; //例外情况 |
| 2573 | |
| 2574 | // 先存储下elem中所有 attr 的名称 |
| 2575 | $.each(attrs, function (key, attr) { |
| 2576 | if (attr && attr.nodeType === 2) { |
| 2577 | attrNames.push(attr.nodeName); |
| 2578 | } |
| 2579 | }); |
| 2580 | // 再根据名称删除所有attr |
| 2581 | $.each(attrNames, function (key, attr) { |
| 2582 | if (exception.indexOf(attr) < 0) { |
| 2583 | // 除了 exception 规定的例外情况,删除其他属性 |
| 2584 | elem.removeAttribute(attr); |
| 2585 | } |
| 2586 | }); |
| 2587 | |
| 2588 | |
| 2589 | // 递归子节点 |
| 2590 | var children = elem.childNodes; |
| 2591 | if (children.length) { |
| 2592 | $.each(children, function (key, value) { |
| 2593 | removeAttrs(value); |
| 2594 | }); |
| 2595 | } |
| 2596 | |
| 2597 | return elem; |
| 2598 | } |
| 2599 | }; |
| 2600 | |
| 2601 | // 绑定 $txt.formatText() 方法 |