使用JavaScript动态添加id

使用JavaScript动态添加id,javascript,jquery,Javascript,Jquery,我正在创建一个Wordpress网站。我正在显示带有查询的帖子。这是输出HTML: <div class="row no-gutters przewodnik-wstep kroki-border row-change-background"> <div class="col-xl-1 jasne-tlo"> </div> <div class="col-xl-4 przewod

我正在创建一个Wordpress网站。我正在显示带有查询的帖子。这是输出HTML:

<div class="row no-gutters przewodnik-wstep kroki-border row-change-background">
    <div class="col-xl-1 jasne-tlo">
    </div> 
    <div class="col-xl-4 przewodnik-tytul green-background obszar-change-background">
        <h3 class="krok text-center materialy-tytul"><?php the_field('nazwa_obszaru_na_stronie_kroku') ?></h3>
    </div>
    <div class="col-xl-6 jasne-tlo przewodnik-tekst krok-1">
        <div class="opis-kroku">
        <?php the_field('zajawka_na_stronie_kroku'); ?>
        </div>
        <a href="<?php echo esc_url(get_field('link_na_stronie_kroku')); ?>"><button class="button-dark czytaj-dalej change-background"><?php the_field('przycisk_na_stronie_kroku'); ?></button></a>
    </div>
    <div class="col-xl-1 jasne-tlo">
    </div>
    </div> 

显然,现在所有的
都在同时更改悬停时的背景。

使用
来获得特定的悬停元素,然后
最接近的
来获得父行
。行更改背景
,然后
查找
来获得div
。obszar更改背景

  $(".row-change-background button").hover(function(){
    var selected = $(this).closest(".row-change-background").find(".obszar-change-background");
    selected.css("background-image", "url(../wp-content/themes/twentytwenty-child/img/KDK_MARBLING_TLO1.png)");
    selected.css("background-size", "cover");
    selected.css("background-repeat", "no-repeat");        
    }, function(){
    var selected = $(this).closest(".row-change-background").find(".obszar-change-background");
    selected.css("background-color", "#c6e4cc");
    selected.css("background-image", "none");  
  });

这回答了你的问题吗?非常感谢。它工作得很好。你知道吗,为什么在Firefox中,当在Chrome浏览器中,页面打开后,背景更改会显示在鼠标悬停上,而在Firefox中,背景更改会显示在鼠标悬停上,而在Chrome浏览器中,背景更改会在页面打开后立即工作?不客气。很难说,因为代码中可能有很多东西。如果你在stackoverflow上搜索,你会看到很多与你类似的问题,试试看。以下是一个开始:
$(".row-change-background").each(function(i) {
  $(this).attr("id", "row-change-background" + i);
});

  $(".row-change-background button").hover(function(){
    $(".row-change-background .obszar-change-background").css("background-image", "url(../wp-content/themes/twentytwenty-child/img/KDK_MARBLING_TLO1.png)");
    $(".row-change-background .obszar-change-background").css("background-size", "cover");
    $(".row-change-background .obszar-change-background").css("background-repeat", "no-repeat");        
    }, function(){
    $(".row-change-background .obszar-change-background").css("background-color", "#c6e4cc");
    $(".row-change-background .obszar-change-background").css("background-image", "none");  
  });
  $(".row-change-background button").hover(function(){
    var selected = $(this).closest(".row-change-background").find(".obszar-change-background");
    selected.css("background-image", "url(../wp-content/themes/twentytwenty-child/img/KDK_MARBLING_TLO1.png)");
    selected.css("background-size", "cover");
    selected.css("background-repeat", "no-repeat");        
    }, function(){
    var selected = $(this).closest(".row-change-background").find(".obszar-change-background");
    selected.css("background-color", "#c6e4cc");
    selected.css("background-image", "none");  
  });