css样式的动态添加及显示和隐藏等零碎用法

(编辑:jimmy 日期: 2024/11/13 浏览:2)

样式的动态添加:

(其中红色字体是动态添加样式的语句),这是一个动态生成无序列表的函数,用来显示搜索到的wifi信号,其中有3个参数,wifi的名称、加密方式、信号强度。

复制代码代码如下:
// create a network list item
function newListItem(network, averageSignalStrength) {
/**
* A Wi-Fi list item has the following HTML structure:
* <li>
* <div>"signalStrength"+ dbm</div>
* <small> Network Security </small>
* <a [class="wifi-secure"]> Network SSID </a>
* </li>
*/
// ssid
var _ = navigator.mozL10n.get;
var ssid = document.createElement('a');
ssid.textContent = network.ssid;
var div = document.createElement('div');
<span style="color:#FF0000;">var str_css ="position: absolute;left: 50%; right: 3rem; top: 45%; padding-top: 0.4rem; font-size: 1.4rem; color: #505859; pointer-events: none;";</span>
<span style="color:#FF0000;">div.style = str_css;</span>
if (!averageSignalStrength)
div.textContent = 'strength:'+ network.signalStrength + 'dbm';
else
div.textContent = 'strength:'+ averageSignalStrength + 'dbm';
// supported authentication methods
var small = document.createElement('small');
var keys = network.capabilities;
if (keys && keys.length) {
small.textContent = _('securedBy', { capabilities: keys.join(', ') });
small.textContent = keys.join(', ');
} else {
small.textContent = _('securityOpen');
small.dataset.l10nId = 'securityOpen';
}
// create list item
var li = document.createElement('li');
li.appendChild(div);
li.appendChild(small);
li.appendChild(ssid);
return li;
}

样式的显示和隐藏:

遍历标签 exp = document.getElementById("exp");

隐藏标签 exp.style.display = "none";

显示标签 exp.style.display = "list-item";

Padding 属性定义元素边框与元素内容之间的空间。

padding 简写属性在一个声明中设置所有内边距属性。设置所有当前或者指定元素内边距属性。该属性可以有1到4个值

复制代码代码如下:
<span style="font-size:14px;">语法结构
padding-top:20px;上内边距
padding-right:30px;右内边距
padding-bottom:30px;下内边距
padding-left:20px;左内边距
padding:1p四边统一内边距
padding:1px1px上下,左右内边距
padding:1px1px1px上,左右,下内边距
padding:1px1px1px1px上,右,下,左内边距</span>