简体   繁体   English

如果ID具有类,则设置另一个元素的属性

[英]If ID has class, set attribute of another element

I'm trying to detect if a class is present and, if so, set the background attribute of another element. 我试图检测是否存在一个类,如果存在,则设置另一个元素的background属性。 This is what I have but it's not working. 这是我所拥有的,但不起作用。

if(jQuery("#slider-banner").hasClass('living-nutrients'))
    {
        jQuery("#home-middle-first").css("background-image","[path to new background image]");
    }

BTW - My next step is for this to detect it whenever the ID "slider-banner" changes, but so far I can't even get it to work once on page load. 顺便说一句-我的下一步是在ID“滑块横幅”更改时立即检测到它,但是到目前为止,我什至无法在页面加载时使它正常工作。 Any help would be greatly appreciated... Thanks! 任何帮助将不胜感激...谢谢!

EDIT: I changed from .attr to .css as instructed. 编辑:我按照指示从.attr更改为.css。 Makes sense... but still not working. 有道理...但仍然无法正常工作。 I've tried adding console.log message within the IF statement and got nothing also. 我尝试在IF语句中添加console.log消息,但也一无所获。 Does that give anyone any more ideas? 那会给任何人更多的想法吗?

Example HTML where class changes: 类更改的示例HTML:

<img id="slider-banner" class="living-nutrients" src="[image path]">

Example HTML where I want to change background image: 我要更改背景图片的HTML示例:

<div class="home-middle-one-third" id="home-middle-first">
        <a href="#"></a>
    </div>

UPDATE: 更新:

For everyone who said it "should work"... you are right! 对于每个说“应该工作”的人……你是对的! Turns out that, as written, it doesn't like being in the footer of the page, but when I moved it to the head, presto! 事实证明,正如所写的那样,它不喜欢位于页面的页脚中,但是当我将其移至页眉时,请记住!

The final piece of this puzzle is to have it detect and evaluate based on the #slider-banner changing, (or more accurately, which class is present for the ID'd area), not just the page loading, as is currently. 这个难题的最后一步是让它根据#slider-banner的更改(或更准确地说,是ID区域中存在的类)进行检测和评估,而不仅仅是像现在这样加载页面。

The ID is for one element of a slide within a slider. 该ID用于滑块中幻灯片的一个元素。 There are three possible classes I could assign to the ID depending on which slide is visible. 根据可见的幻灯片,我可以为ID分配三种可能的类。 So I need the script to evaluate every time a slide changes. 因此,每次幻灯片更改时,我都需要脚本来评估。

Any ideas? 有任何想法吗? Thank you all! 谢谢你们!

background-image is a element's style property, not its own one. background-image是元素的样式属性,而不是其自己的属性。

So .css("background-image","[path to new background image]"); 因此.css("background-image","[path to new background image]");

Almost! 几乎!

if(jQuery("#slider-banner").hasClass('living-nutrients'))
{
    jQuery("#home-middle-first").css("background-image","[path to new background image]");
}

css is the correct function to set a CSS attribute. css是设置CSS属性的正确函数。
The attr will set an HTML attribute. attr将设置HTML属性。 <div attr='attr value'>

Edit 编辑
I'm kind of guessing about the functionality of your script here in the following example. 在以下示例中,我有点猜测您的脚本的功能。
When you set the background-image of a HTML node, that's all it does is set the background image. 当您设置background-image一个HTML节点,这就是它的作用是设置背景图片。 You must also set the width and height accordingly, to all the node to be large enough to even see the background of the node. 您还必须相应地将所有节点的宽度和高度设置为足够大,甚至可以看到节点的背景。 Background images will not automatically resize the node. 背景图像不会自动调整节点的大小。

var slider = jQuery("#slider-banner"); // jQuery("#slider-banner") is slow, so we save it to a var if we use it more than once
console.log(slider); // should be this in Chrome: [<img id="slider-banner" class="living-nutrients" src="[image path]">]
if(slider.hasClass('living-nutrients'))
{
    jQuery("#home-middle-first").css({
        "background-image":"url("+slider.attr('src')+")", // url() for good meassures
        //"background-image":slider.css('background-image'), //try this if that doesn't work
        "height":slider.height(),
        "width":slider.width()
    });
}

Here is a working example. 这是一个工作示例。

尝试这个

jQuery("#home-middle-first").css("background-image","url([path])");

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

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