HTML5 details元素的功能检测和样式

As I discussed in the article that introduced the tags, the details and summary elements are excellent ways to create accordion interfaces on web pages. Until recently, features like this have required a mass of JavaScript. (Indeed, frameworks like JQuery UI have entire modules devoted to supporting them).

正如我在介绍标签文章中所讨论的那样, detailssummary元素是在网页上创建手风琴界面的绝佳方法。 直到最近,像这样的功能都需要大量的JavaScript 。 (实际上,像JQuery UI这样的框架具有用于支持它们的整个模块)。

As I mentioned in the previous article, there are a few issues to using details today, mostly around lack of support in and Microsoft Edge. Thankfully, these are easy to solve with a combination of and a little JavaScript feature detection and DOM manipulation.

正如我在上一篇文章中提到的那样,今天使用details存在一些问题,主要与缺少Microsoft Edge和Microsoft Edge支持有关。 值得庆幸的是,将与少量JavaScript功能检测和DOM操作相结合,即可轻松解决这些问题。

HTML (The HTML)

<details id="det-container">
    <summary>Mainframe</summary>
    <a href="#">Park</a>
    <a href="#">Hosts</a>
    <a href="#">Storylines</a>
    <a href="#">Recalls</a>
</details>

添加功能检测支持 (Adding support with feature detection)

Placed at the bottom of your page, the script does two things. First, it initiates a simple feature check by attempting to create a <details> element with an open attribute in the DOM. If the check fails, the script adds a no-det class to the <details> element, with user clicks on the <summary> child of this element adding and removing an open attribute:

该脚本位于页面底部,可完成两件事。 首先,它通过尝试在DOM中创建具有open属性的<details>元素来启动简单的功能检查。 如果检查失败,则脚本会在<details>元素中添加一个no-det类,用户单击该元素的<summary>子元素会添加并删除一个open属性:

function supports_details() {
	if (!('open' in document.createElement('details'))) {
		return "no-details"; }
	}
function switchOpen() {
	if (details.getAttribute('open')) {
        details.removeAttribute('open');
	} else {
        details.setAttribute('open', 'open');
	}
}
if (supports_details() == "no-details" ) {
	var details = document.getElementById("det-container");
	var summary = details.firstElementChild;
    details.classList.add("no-det");
    summary.addEventListener("click", switchOpen, false);
}
supports_details;

添加样式 (Adding style)

You can apply CSS to the details and <summary> elements in the same ways that you can to everything else. In this case, I'm using the addition of the no-det class to create an equivalent UI element for non-supporting browsers:

您可以将CSS应用于details<summary>元素,就像对其他所有元素一样。 在这种情况下,我使用了no-det类的添加来为不支持的浏览器创建等效的UI元素:

summary, details, details a {
	display: block;
}
details.no-det summary::before { 
  content: "►";
  padding-right: 5px;
  font-size: 1.2em;
  transition: .6s linear;
  display: inline-block;
}
details.no-det[open] summary::before { 
  transform: rotate(90deg); 
}
details.no-det a {
  display: none;
}
details[open].no-det a { 
  display: block;
}

This duplicates the functionality of the <details> and <summary> elements for all browsers. I’ve added a little more CSS to the version you can see on the associated CodePen.

这将复制所有浏览器的<details><summary>元素的功能。 我在相关CodePen上看到的版本中添加了更多CSS。

Maksim Chemerisuk detailed the creation of a very thorough polyfill for the <details> element that takes care of edge cases and IE8.

Maksim Chemerisuk详细介绍了<details>元素创建非常彻底的 polyfill的方法,该方法可以处理边缘情况和IE8。

翻译自: https://thenewcode.com/680/Feature-Detection-and-Styling-For-The-HTML5-details-Element

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML页面调试的常用方法有以下几种: 1. 使用浏览器自带的开发者工具:大多数现代浏览器都内置了开发者工具,可以通过按下F12键或者右键点击页面并选择“检查元素”来打开。开发者工具提供了丰富的调试功能,包括查看和编辑HTML、CSSJavaScript代码,监视网络请求和响应,以及调试JavaScript代码等。可以使用开发者工具来检查页面的结构、样式和脚本,并定位问题所在。 2. 在JavaScript代码中设置断点:在JavaScript代码中使用`debugger`语句可以在特定位置设置断点,当代码执行到断点处时会暂停执行并进入调试模式。可以使用断点来逐行调试代码,查看变量的值、执行流程和错误信息,以便定位和修复问题。 3. 使用console.log()输出调试信息:通过在JavaScript代码中使用`console.log()`函数可以在浏览器的控制台输出调试信息。可以在关键位置插入`console.log()`语句,输出变量的值或执行过程中的状态,以便观察代码的运行情况和检查问题。 4. 使用在线工具进行调试:除了浏览器自带的调试工具,还有一些在线工具可以帮助调试HTML页面。例如,JSFiddle、CodePen和JSBin等在线代码编辑器提供了实时预览和调试功能,可以方便地编辑和测试HTML、CSSJavaScript代码。 总之,HTML页面调试常用的方法包括使用浏览器自带的开发者工具、在JavaScript代码中设置断点、使用console.log()输出调试信息,以及使用在线工具进行调试。根据具体的需求和情况,选择适合自己的调试方法来定位和解决问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [HTML 调试](https://blog.csdn.net/maimang1001/article/details/114364390)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [学会如何调试html代码](https://blog.csdn.net/weixin_40301728/article/details/122954397)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值