gpt4 book ai didi

javascript - 在选择更改时编写 javascript 隐藏/显示 div 的更好方法

转载 作者:行者123 更新时间:2023-12-02 17:29:25 25 4
gpt4 key购买 nike

有没有更好的方法来写这样的东西。我有几个 Div,其中包含我想要根据下拉列表中的选择显示的列表。为了不必更新每个 block 以隐藏然后显示我想要的 block ,是否有一种通配符方法或方法我可以简单地使用定义我的 Div 的数组来循环隐藏/显示?

我正在构建的基础。计划转换为 switch 语句而不是一系列 If 语句。

我主要不喜欢这部分。

document.getElementById('my1').style.display = 'block';
document.getElementById('my2').style.display = 'none';
document.getElementById('my3').style.display = 'none';

http://jsfiddle.net/ak95K/3/

最佳答案

如果我需要支持较旧的(但不是古老的)浏览器(并且没有使用 jQuery),就像你说的那样,那么我会做这样的事情。

CSS

.hide {
display: none;
}

HTML

<select id="mine">
<option value="0">Test1</option>
<option value="1">Test2</option>
</select>
<div id="display1">
<div>
<p>Some Text in 1</p>
</div>
<div class="hide">
<p>Some Text in 2</p>
</div>
</div>

Javascript跨浏览器支持代码

(function () {
var slice = [].slice,
nativeTrim = ''.trim,
trim,
classList;

function isFunction(arg) {
return typeof arg === 'function';
}

function isString(arg) {
return typeof arg === 'string';
}

function handler(object, evt, func) {
var ret;

if (evt) {
ret = func.call(object, evt);
if (false === ret) {
evt.stopPropagation();
evt.preventDefault();
}
} else {
window.event.target = window.event.srcElement;
ret = func.call(object, window.event);
if (false === ret) {
window.event.returnValue = false;
window.event.cancelBubble = true;
}
}

return ret;
}

function addEventListener(object, type, func) {
var uid = type + ':' + func,
euid = 'e:' + uid;

object[euid] = func;
if (isFunction(object.addEventListener)) {
object[uid] = function (evt) {
handler(object, evt, object[euid]);
};

object.addEventListener(type, object[uid], false);
} else if (object.attachEvent) {
object[uid] = function () {
handler(object, null, object[euid]);
};

object.attachEvent('on' + type, object[uid]);
} else {
throw new Error('Handler could not be added.');
}
}

if (isFunction(nativeTrim)) {
trim = function (text) {
return nativeTrim.call(text);
};
} else {
trim = function (text) {
return text.replace(/^\s+|\s+$/g, '');
};
}

if ('classList' in document.body) {
classList = {
contains: function (node, className) {
return node.classList.contains(className);
},

add: function add(node, className) {
node.classList.add(className);
},

remove: function (node, className) {
node.classList.remove(className);
},

toggle: function (node, className) {
node.classList.toggle(className);
}
};
} else {
classList = {
makeRegex: function (className, flags) {
return new RegExp('(?:^|\\s)' + className + '(?!\\S)', isString(flags) ? flags : '');
},

contains: function (node, className) {
return !!node.className.match(classList.makeRegex(className));
},

add: function add(node, className) {
if (!classList.contains(node, className)) {
node.className = trim(node.className);
if (node.className) {
node.className += ' ';
}

node.className += className;
}
},

remove: function (node, className) {
if (classList.contains(node, className)) {
node.className = trim(node.className.replace(classList.makeRegex(className, 'g'), ''));
}
},

toggle: function (node, className) {
if (classList.contains(node, className)) {
classList.remove(node, className);
} else {
classList.add(node, className);
}
}
};
}

window.$ = {
addEventListener: addEventListener,
classList: classList
};
}());

Javascript 工作代码

$.addEventListener(document.getElementById('mine'), 'change', (function () {
var children1 = document.getElementById('display1').children,
length1,
index;

return function (evt) {
for (index = 0, length1 = children1.length; index < length1; index += 1) {
$.classList.toggle(children1[index], 'hide');
}
};
}()));

关于jsFiddle

更新:添加选项

CSS

<select id="mine">
<option value="test1">Test1</option>
<option value="test2">Test2</option>
<option value="test3">Test3</option>
</select>
<div id="display1">
<div>
<p>Some Text in 1</p>
</div>
<div class="hide">
<p>Some Text in 2</p>
</div>
<div class="hide">
<p>Some Text in 3</p>
</div>
</div>

Javascript

$.addEventListener(document.getElementById('mine'), 'change', (function () {
var children1 = document.getElementById('display1').children,
length1,
index;

return function (evt) {
for (index = 0, length1 = children1.length; index < length1; index += 1) {
if (index === evt.target.selectedIndex) {
$.classList.remove(children1[index], 'hide');
} else {
$.classList.add(children1[index], 'hide');
}
}
};
}()));

关于jsFiddle

如果您编写的内容不需要支持旧版浏览器,那么它看起来会像这样。

Javascript

document.getElementById('mine').addEventListener('change', (function () {
var children1 = document.getElementById('display1').children,
length1,
index;

return function (evt) {
for (index = 0, length1 = children1.length; index < length1; index += 1) {
if (index === evt.target.selectedIndex) {
children1[index].classList.remove('hide');
} else {
children1[index].classList.add('hide');
}
}
};
}()), false);

关于jsFiddle

关于javascript - 在选择更改时编写 javascript 隐藏/显示 div 的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23206385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com