简体   繁体   English

使用javascript或jquery更改内联样式

[英]change inline style with javascript or jquery

I am trying to change an inline style on my squarespace site that is automatically set by one of the style options on page load. 我正在尝试更改我的Squarespace网站上的内联样式,该样式由页面加载时的样式选项之一自动设置。 I want to change the height specifically of the background images, I am trying with several different methods but none have an effect. 我想专门更改背景图像的高度,我正在尝试几种不同的方法,但是都没有效果。 If I go and make the change manually then it comes out just how I want. 如果我去手动进行更改,那么它就是我想要的。 I tried searching but for some reason nothing I've found is working. 我尝试搜索,但是由于某种原因,我什么都没找到。

trying to change the banner on this page here: https://saffron-hawk-jh9a.squarespace.com/applications/ 尝试在此处更改此页面上的横幅: https : //saffron-hawk-jh9a.squarespace.com/applications/

you can see the black space where the banner images should be. 您可以看到横幅图像应在的黑色空间。 I console.logged the element collection, so they are there and that's where i found the property for csstext. 我console.logged元素集合,所以它们在那里,这就是我找到csstext属性的地方。 Any help greatly appreciated! 任何帮助,不胜感激!

//jquery
$('.Index-gallery-item').css({ height: "calc(100vh - 80px;)"} );

/js
document.addEventListener("DOMContentLoaded", function(event) { 

var els = document.getElementsByClassName("Index-gallery-item");
console.log(els);

for ( var i=0; i< els.length ; i++){
console.log(i);

document.getElementsByClassName("Index-gallery-item")[i].setAttribute("style", "height:calc(100vh - 80px);");

els[i].style.cssText = "height : calc(100vh - 80px);" ;
els[i].style.height = "calc(100vh - 80px);" ;

}


});

It's because getElementByClassname returns a HTMLCollection and querySelectorAll returns a NodeList. 这是因为getElementByClassname返回HTMLCollection,而querySelectorAll返回NodeList。 You can't set style of HTMLCollection item, only get it (I guess) 您不能设置HTMLCollection项目的样式,只能获取它(我想)

var items = document.querySelectorAll('.Index-gallery-item');

[].forEach.call(items, function(item) {
    item.style.height = 'calc(100vh - 80px)'; 
});

The issue appears to be related to the parent element with the class of Index-gallery-item-inner is sized with a height that is less than what you are trying to make the images. 问题似乎与父元素有关,其Index-gallery-item-inner类的高度设置为小于您要制作图像的高度。 You should be able to fix this with either a style or a javascript command 您应该可以使用样式或javascript命令来解决此问题

//css
.Index-gallery-item-inner { height: calc(100vh - 80px); }

//js
document.querySelectorAll('.Index-gallery-item-inner').forEach(image => { image.style.height = 'calc(100vh - 80px)'; });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM