简体   繁体   English

Ajax Javascript 不会更改我的 CSS 而无需刷新页面

[英]Ajax Javascript doesn't change my CSS without page refresh

I am changing my CSS styling using JavaScript code but it doesn't work when my search filter finds that element;我正在使用 JavaScript 代码更改我的 CSS 样式,但是当我的搜索过滤器找到该元素时它不起作用; the style effect doesn't change until I refresh the page.在我刷新页面之前,样式效果不会改变。 Here is my code, it works perfectly when I refresh the page, but doesn't work dynamically without reloading the page.这是我的代码,当我刷新页面时它工作得很好,但如果不重新加载页面就不能动态工作。

jQuery(function($) {
  $( ".card-auto-label:contains('Ny')" ).css( "background", "#5dd018" );
  $( ".card-auto-label:contains('Solgt')" ).css( "background", "red" );
  $( ".card__label:contains('Ny')" ).css( "background", "#5dd018" , "!important");
});

Your code only runs once, and it does not add CSS to the page globally, it adds it to the exact elements specified that already exist on the page.您的代码只运行一次,它不会将 CSS 全局添加到页面中,而是将其添加到页面上已经存在的指定元素中。

Unlike using a <style> tag or a css file, applying styles with the jQuery css() function adds them on to the specific elements you target as inline css. Unlike using a <style> tag or a css file, applying styles with the jQuery css() function adds them on to the specific elements you target as inline css.

For example, $(".someclass").css("mystyles")例如, $(".someclass").css("mystyles")

Has a result that looks like this: <div class="someclass" style="mystyle"></div>结果如下所示: <div class="someclass" style="mystyle"></div>

This isn't the same as a css style globally in your page that looks like this:这与您页面中的全局 css 样式不同,如下所示:

.someclass{ mystyle }

In the first example, your style is applied ONLY to the element that you assigned it to with jQuery.在第一个示例中,您的样式仅应用于您使用 jQuery 分配给它的元素。 If you add another element to the page with the exact same class and attributes, it will not inherit the style.如果您将另一个元素添加到具有完全相同的 class 和属性的页面,它将不会继承样式。 There is nothing in the page telling it to be styled that way.页面中没有任何内容告诉它以这种方式设置样式。

In the second example, the style is globally applied to every element with class someclass .在第二个示例中,该样式全局应用于每个具有 class someclass的元素。 It doesn't matter when the element is added to the page, onload or dynamically created later, the browser will apply it to any instance of someclass that it seems in the DOM.无论何时将元素添加到页面、onload 或稍后动态创建,浏览器都会将其应用于它在 DOM 中出现的someclass的任何实例。

Since you are relying on a jQuery function to determine where to put the styles, and that function cannot be replicated with css alone, I can see why the second example won't work for you. Since you are relying on a jQuery function to determine where to put the styles, and that function cannot be replicated with css alone, I can see why the second example won't work for you. So to fix this issue, you need to have a function that triggers when your AJAX call finishes loading.因此,要解决此问题,您需要有一个 function 在您的 AJAX 调用完成加载时触发。 I assume you are already doing something with the data from the AJAX call to add it to your page?我假设您已经在使用 AJAX 调用中的数据将其添加到您的页面? Add the javascript code inside that function AFTER the content is added to the page and they will be updated with the styles.将内容添加到页面后,在 function 中添加 javascript 代码,它们将使用 styles 进行更新。 The way you have it written now, it is just firing once on page load.你现在写它的方式,它只是在页面加载时触发一次。

From the code you provided it seems that you are only applying the styling when you load the page.从您提供的代码看来,您只是在加载页面时才应用样式。 If you then later add more elements you want to style to the page they will not have the styling applied.如果您稍后向页面添加更多要设置样式的元素,它们将不会应用样式。

This is because when you write这是因为当你写

$( ".card-auto-label:contains('Ny')" ).css( "background", "#5dd018" );

it writes the style into the elements HTML style property like so它将样式写入元素 HTML 样式属性,如下所示

<div style="background: #5dd018">Ny</div>

making it only apply to that element.使其仅适用于该元素。 That's also why it works when you reload the page, because now the elements are on the page when your code runs, and the styling gets applied.这也是当您重新加载页面时它可以工作的原因,因为现在当您的代码运行时元素在页面上,并且样式被应用。

You need to put your code in a function您需要将代码放入 function

function applyStyling() {
  $( ".card-auto-label:contains('Ny')" ).css( "background", "#5dd018" );
  $( ".card-auto-label:contains('Solgt')" ).css( "background", "red" );
  $( ".card__label:contains('Ny')" ).css( "background", "#5dd018" , "!important");
}

and then call that function every time after you add new elements to the page.然后在每次向页面添加新元素调用 function 。

A better solution would be to put the styles in a CSS class and then add that class to the HTML elements you want to add the styling to. A better solution would be to put the styles in a CSS class and then add that class to the HTML elements you want to add the styling to.

.Solgt {
  background: red;
}

.Ny {
  background: #5dd018;
}
function applyStyling() {
  $( ".card-auto-label:contains('Ny')" ).addClass('Ny');
  $( ".card-auto-label:contains('Solgt')" ).addClass('Solgt');
  $( ".card__label:contains('Ny')" ).addClass('Ny');
}

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

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