Chemistry 101 - Part 3 Organic and Physical Chemistry

An introductory course for anybody who wants to study chemistry - either towards future pathways or just for fun!
4.31 (65 reviews)
Udemy
platform
English
language
Science
category
instructor
Chemistry 101 - Part 3 Organic and Physical Chemistry
845
students
2 hours
content
Aug 2013
last update
$49.99
regular price

Why take this course?

根据您提供的HTML结构,这段代码是用于在一个无序列表<ul>中动态生成一系列带有不同样式的<li>元素。每个<li>元素都包含了一个强调(<strong>)标签,用于突出显示列表项中的文本。

<ul id="myList">
  <li><a href="#item1" onclick="highlightItem(this)">Item 1</a></li>
  <li><a href="#item2" onclick="highlightItem(this)">Item 2</a></li>
  <!-- 以此类推,可以添加更多的列表项 -->
</ul>

每个列表项(<li>)都包含一个超链接(<a>),当点击时会触发highlightItem函数。这个函数的作用是更改选中列表项的样式,使其在页面上突出显示。

function highlightItem(element) {
  var items = document.querySelectorAll('#myList li');
  items.forEach(function(item) {
    item.style.backgroundColor = ''; // 清除所有列表项的背景颜色
    item === element ? item.style.backgroundColor = 'yellow' : ''; // 将当前点击的元素的背景颜色设置为黄色
  });
}

这个函数首先查询ID为myList的无序列表下所有的<li>元素,然后遍历它们并清除他们的背景颜色。接着,它比较当前执行highlightItem函数的元素是否与点击的元素相同,如果相同,则将该元素的背景颜色设置为黄色(或者任何你想要的样式)。

请注意,这个JavaScript代码片段依赖于有一个onclick属性在每个列表项的<a>标签上。在现代Web开发中,通常不建议使用onclick attribute来处理事件,而是使用更现代的事件监听机制(如addEventListener)。下面是使用addEventListener的类似实现:

<ul id="myList">
  <li><a href="#item1" data-list-item="1">Item 1</a></li>
  <li><a href="#item2" data-list-item="2">Item 2</a></li>
  <!-- 以此类推,可以添加更多的列表项 -->
</ul>

并且在JavaScript中:

document.querySelectorAll('#myList a').forEach(function(link) {
  link.addEventListener('click', function(e) {
    e.preventDefault(); // 阻止默认的链接行为
    var listItem = document.querySelector('li[data-list-item="' + this.getAttribute('data-list-item') + '"]');
    highlightItem(listItem);
  });
});

function highlightItem(element) {
  var items = document.querySelectorAll('#myList li');
  items.forEach(function(item) {
    item.style.backgroundColor = ''; // 清除所有列表项的背景颜色
    item === element ? item.style.backgroundColor = 'yellow' : ''; // 将当前点击的元素的背景颜色设置为黄色
  });
}

这种方法避免了使用onclick attribute,并且提供了更清晰的事件处理逻辑。

Course Gallery

Chemistry 101 - Part 3 Organic and Physical Chemistry – Screenshot 1
Screenshot 1Chemistry 101 - Part 3 Organic and Physical Chemistry
Chemistry 101 - Part 3 Organic and Physical Chemistry – Screenshot 2
Screenshot 2Chemistry 101 - Part 3 Organic and Physical Chemistry
Chemistry 101 - Part 3 Organic and Physical Chemistry – Screenshot 3
Screenshot 3Chemistry 101 - Part 3 Organic and Physical Chemistry
Chemistry 101 - Part 3 Organic and Physical Chemistry – Screenshot 4
Screenshot 4Chemistry 101 - Part 3 Organic and Physical Chemistry

Loading charts...

Related Topics

83434
udemy ID
25/08/2013
course created date
22/11/2019
course indexed date
Bot
course submited by