[英]How to avoid repeating code when selecting similar DOM elements?
Currently I have the following:目前我有以下内容:
const mainBannerInfo = document.querySelector('.bigfullbanner').querySelector('.banner-row').querySelector('.info-col');
const title__MainBanner = mainBannerInfo.querySelector('h4');
const subtitle__MainBanner = mainBannerInfo.querySelector('p');
const buttonCTA__MainBanner = mainBannerInfo.getElementsByClassName('button-big-banner');
let watchMedia = window.matchMedia("( max-width: 1023px )")
function whiteToBlackInfo__MainBanner() {
//Changing TITLE text styles
title__MainBanner.classList.remove('text-white');
title__MainBanner.classList.add('text-black');
//Changing SUBTITLE styles
subtitle__MainBanner.classList.remove('text-white');
subtitle__MainBanner.classList.add('text-black');
//Changing CTA Buttons styles
for(let i = 0; i < buttonCTA__MainBanner.length; i++) {
buttonCTA__MainBanner[i].classList.remove('text-black');
buttonCTA__MainBanner[i].classList.add('text-white');
buttonCTA__MainBanner[i].classList.remove('bg-white');
buttonCTA__MainBanner[i].classList.add('bg-black');
}
}
function blackToWhiteInfo__MainBanner() {
//Changing TITLE text styles
title__MainBanner.classList.remove('text-black');
title__MainBanner.classList.add('text-white');
//Changing SUBTITLE styles
subtitle__MainBanner.classList.remove('text-black');
subtitle__MainBanner.classList.add('text-white');
//Changing CTA Buttons styles
for(let i = 0; i < buttonCTA__MainBanner.length; i++) {
buttonCTA__MainBanner[i].classList.remove('text-white');
buttonCTA__MainBanner[i].classList.add('text-black');
buttonCTA__MainBanner[i].classList.remove('bg-black');
buttonCTA__MainBanner[i].classList.add('bg-white');
}
}
if(
mainBannerInfo.classList.contains('bottomright') ||
mainBannerInfo.classList.contains('bottomleft') ||
mainBannerInfo.classList.contains('topright') ||
mainBannerInfo.classList.contains('topleft') ||
mainBannerInfo.classList.contains('centerInfo') &&
!mainBannerInfo.classList.contains('keep-color')) {
blackToWhiteInfo__MainBanner();
if(
mainBannerInfo.classList.contains('outsideDesktop') &&
watchMedia.matches == false
) {
whiteToBlackInfo__MainBanner();
} else if(
mainBannerInfo.classList.contains('outsideMobile') &&
watchMedia.matches == true
) {
whiteToBlackInfo__MainBanner();
}
}
// -------------------------------------------------
I'm currently working on an CMS and my task is to help the Content responsible person by automating the changes of styles of some banners so he only has to write/remove some classes in the html file.我目前正在开发 CMS,我的任务是通过自动更改某些横幅的 styles 来帮助内容负责人,因此他只需在 html 文件中写入/删除一些类。 So I created the DOM Selectors and created some functions to achieve this.
所以我创建了 DOM 选择器并创建了一些函数来实现这一点。 The thing is I have another banner almost identical:
问题是我有另一个几乎相同的横幅:
const novedadBannerInfo = document.querySelector('.smallfullbanner').querySelector('.banner-row').querySelector('.info-col');
const title__novedadBanner = novedadBannerInfo.querySelector('h4');
const subtitle__novedadBanner = novedadBannerInfo.querySelector('p');
const buttonCTA__novedadBanner = novedadBannerInfo.getElementsByClassName('button-big-banner');
function whiteToBlackInfo__novedadBanner() {
//Changing TITLE text styles
title__novedadBanner.classList.remove('text-white');
title__novedadBanner.classList.add('text-black');
//Changing SUBTITLE styles
subtitle__novedadBanner.classList.remove('text-white');
subtitle__novedadBanner.classList.add('text-black');
//Changing CTA Buttons styles
for(let i = 0; i < buttonCTA__novedadBanner.length; i++) {
buttonCTA__novedadBanner[i].classList.remove('text-black');
buttonCTA__novedadBanner[i].classList.add('text-white');
buttonCTA__novedadBanner[i].classList.remove('bg-white');
buttonCTA__novedadBanner[i].classList.add('bg-black');
}
}
function blackToWhiteInfo__novedadBanner() {
//Changing TITLE text styles
title__novedadBanner.classList.remove('text-black');
title__novedadBanner.classList.add('text-white');
//Changing SUBTITLE styles
subtitle__novedadBanner.classList.remove('text-black');
subtitle__novedadBanner.classList.add('text-white');
//Changing CTA Buttons styles
for(let i = 0; i < buttonCTA__novedadBanner.length; i++) {
buttonCTA__novedadBanner[i].classList.remove('text-white');
buttonCTA__novedadBanner[i].classList.add('text-black');
buttonCTA__novedadBanner[i].classList.remove('bg-black');
buttonCTA__novedadBanner[i].classList.add('bg-white');
}
}
if(
novedadBannerInfo.classList.contains('bottomright') ||
novedadBannerInfo.classList.contains('bottomleft') ||
novedadBannerInfo.classList.contains('topright') ||
novedadBannerInfo.classList.contains('topleft') ||
novedadBannerInfo.classList.contains('centerInfo') &&
!mainBannerInfo.classList.contains('keep-color')) {
blackToWhiteInfo__novedadBanner();
if(
novedadBannerInfo.classList.contains('outsideDesktop') &&
watchMedia.matches == false
) {
whiteToBlackInfo__novedadBanner();
} else if(
novedadBannerInfo.classList.contains('outsideMobile') &&
watchMedia.matches == true
) {
whiteToBlackInfo__novedadBanner();
}
}
// -------------------------------------------------
As you can see it is practically the same but the variables storing the selectors have to change and I have to create all this functions all over again.如您所见,它实际上是相同的,但存储选择器的变量必须更改,我必须重新创建所有这些函数。
Is there any way to shrink this code?有没有办法缩小这段代码?
You can create a helper function mainBanner(color1, color2)
with two colors as parameters.您可以使用两个 colors 作为参数创建一个帮助器 function
mainBanner(color1, color2)
。 Inside this function, you can use color1
and color2
to build your classes (like text-white, text-black, ...) dynamically.在这个 function 中,您可以使用
color1
和color2
动态构建您的类(如 text-white、text-black 等)。
See this code:请参阅此代码:
function mainBanner(color1, color2) {
//Changing TITLE text styles
title__MainBanner.classList.remove('text-' + color1);
title__MainBanner.classList.add('text-' + color2);
//Changing SUBTITLE styles
subtitle__MainBanner.classList.remove('text-' + color1);
subtitle__MainBanner.classList.add('text-' + color2);
//Changing CTA Buttons styles
for(let i = 0; i < buttonCTA__MainBanner.length; i++) {
buttonCTA__MainBanner[i].classList.remove('text-' + color2);
buttonCTA__MainBanner[i].classList.add('text-' + color1);
buttonCTA__MainBanner[i].classList.remove('bg-' + color1);
buttonCTA__MainBanner[i].classList.add('bg-' + color2);
}
}
function blackToWhiteInfo__MainBanner() {
mainBanner('black', 'white');
}
function whiteToBlackInfo__MainBanner() {
mainBanner('white', 'black');
}
If you have more options than the both colors, you can pass an options
object to the mainBanner(options)
function and implement it like this:如果您有比 colors 更多的选项,您可以将
options
object 传递给mainBanner(options)
function 并像这样实现它:
function mainBanner(options) {
var color1 = options.color1;
var color2 = options.color2;
var textClsPrefix = options.textClsPrefix;
var someOtherProperty = options.someOtherProperty;
...
}
// sample call 1
mainBanner({
color1: "white",
color2: "black",
textClsPrefix: "text-",
someOtherProperty: "some-value"
});
// sample call 2
mainBanner({
color1: "black",
color2: "white",
textClsPrefix: "text-light-",
someOtherProperty: "some-value-2"
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.