gpt4 book ai didi

php - 是否可以在不刷新标签的情况下使用 ajax 仅更新 css 以及如何更新?

转载 作者:行者123 更新时间:2023-11-28 05:26:13 26 4
gpt4 key购买 nike

有什么方法可以在不刷新内容的情况下使用 php 或 ajax 只更新页面内容样式?这里的内容我指的是 html 标签及其内容,除了为它定义的样式。样式应来自 CSS,并应使用类名和 ID 链接到标签。

如果可能,任何人都请指定完成此操作的提示或方法。最好只使用 php/ajax 和 css,不要使用其他语言,如 javacsript 或 jQuery。

我知道如何使用 jQuery 和 javascript 实现这一目标。

简而言之,我只需要更新 html 标签的 css(而不是标签),动态使用 ajax 或 php。是否可以?如果有的话,请告诉我怎么做?

PHP 和 AJAX 是我用来显示内容的语言,稍后我将使用 AJAX 来更新内容。 PHP 可用于更新或识别类名、样式表等。

最佳答案

如果您想在不重新加载页面的情况下动态更改样式表,您将必须使用 JavaScript。

有关最简单的答案,请参阅 this question所以:

Add an id attribute to the CSS link tag to manipulate the tag using JavaScript:

<link id="cssfile" href="css/avocado.css" type="text/css" rel="stylesheet">

The Javascript to set the href attribute resembles:

document.getElementById('cssfile').href = 'css/carrot.css';

Colours could be tweaked by the user, by clicking a link:

<a href="#" onclick="document.getElementById('cssfile').href='css/carrot.css';">Carrots</a>

By changing the media type, this could also allow users to quickly change print layouts, the preferred layout on mobiles (or tablets), and more.

This solution does not require jQuery.

This website还有一个很好的脚本可以使用。它增加了一点复杂性和将用户使用的样式表保存在 cookie 中的功能

如果你使用 jQuery,你可以使用 this plugin

此插件旨在模仿 jQuery 的 jQuery.get() AJAX 调用,使用起来非常简单

 $.getCSS("getCSS.css");

参见 the docs了解更多信息。


此外,this SO answer 是一个非常的好资源,并提供了有关如何创建动态样式表切换器的分步教程。

关于php - 是否可以在不刷新标签的情况下使用 ajax 仅更新 css 以及如何更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32171734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com