File tree Expand file tree Collapse file tree
2-ui/1-document/06-dom-attributes-and-properties Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77 <div data-widget-name =" menu" >Choose the genre</div >
88
99 <script >
10- // getting it
10+ // 获取它
1111 let elem = document .querySelector (' [data-widget-name]' );
1212
13- // reading the value
13+ // 读取值
1414 alert (elem .dataset .widgetName );
15- // or
15+ // 或
1616 alert (elem .getAttribute (' data-widget-name' ));
1717 </script >
1818</body >
Original file line number Diff line number Diff line change 11
2- 首先,我们需要找到所有外部引用 。
2+ 首先,我们需要找到所有外部链接 。
33
4- 这里有两种办法 。
4+ 这里有两种方式 。
55
6- 第一种是使用 ` document.querySelectorAll('a') ` 然后过滤出我们需要的那部分 :
6+ 第一种是使用 ` document.querySelectorAll('a') ` 找到所有链接,然后过滤出我们需要的部分 :
77
88``` js
99let links = document .querySelectorAll (' a' );
@@ -12,22 +12,22 @@ for (let link of links) {
1212* ! *
1313 let href = link .getAttribute (' href' );
1414*/ ! *
15- if (! href) continue ; // no attribute
15+ if (! href) continue ; // 没有特性
1616
17- if (! href .includes (' ://' )) continue ; // no protocol
17+ if (! href .includes (' ://' )) continue ; // 没有协议
1818
19- if (href .startsWith (' http://internal.com' )) continue ; // internal
19+ if (href .startsWith (' http://internal.com' )) continue ; // 内部的
2020
2121 link .style .color = ' orange' ;
2222}
2323```
2424
25- 请注意:我们用的是 ` link.getAttribute('href') ` 。而不是 ` link.href ` ,因为我们需要的是 HTML 上的值 。
25+ 请注意:我们用的是 ` link.getAttribute('href') ` 。而不是 ` link.href ` ,因为我们需要的是来自 HTML 的值 。
2626
27- ……除此之外,有一个更简便的方式是利用 CSS 选择器的伪类选择器 :
27+ ……另一种更简单的方法,是使用 CSS 选择器进行检查 :
2828
2929``` js
30- // 查找所有 href 中包含: // 的链接
30+ // 查找所有 href 中包含 : // 的链接
3131// 但 href 不是以 http://internal.com 开头
3232let selector = ' a[href*="://"]:not([href^="http://internal.com"])' ;
3333let links = document .querySelectorAll (selector);
Original file line number Diff line number Diff line change @@ -2,12 +2,12 @@ importance: 3
22
33---
44
5- # 使外来链接变橙色
5+ # 将外部链接设为橙色
66
7- 改变所有外来链接的 ` style ` 属性,使链接变橙色 。
7+ 通过修改 ` style ` 属性,将所有外部链接变为橙色 。
88
9- 如果一个链接是外来的 :
10- - 这个 ` href ` 包含有 ` :// `
9+ 如果一个链接是外部的 :
10+ - 其 ` href ` 中包含 ` :// `
1111- 但不是以 ` http://internal.com ` 开头。
1212
1313例如:
@@ -24,12 +24,12 @@ importance: 3
2424</ul >
2525
2626<script >
27- // 简单地为这些链接设置样式
27+ // 为单个链接设置样式
2828 let link = document .querySelector (' a' );
2929 link .style .color = ' orange' ;
3030 </script >
3131```
3232
33- 结果应该是这样的 :
33+ 结果应该是 :
3434
3535[ iframe border=1 height=180 src="solution"]
You can’t perform that action at this time.
0 commit comments