简体   繁体   English

如何使用JavaScript或jQuery更改另一页面上元素的样式?

[英]How to change style of an element on another page using JavaScript or jQuery?

Ok, this is my problem... I have two pages. 好的,这是我的问题......我有两页。 On the first page I have a header ( h2 ) element on which I want to apply a border style. 在第一页上,我有一个标题( h2 )元素,我想在其上应用边框样式。 On the second page, there is only one link. 在第二页上,只有一个链接。 When clicking on the link from the second page, to navigate to the first page, it should create a border on the first page around the h2 tag. 当点击第二页的链接时,要导航到第一页,它应该在h2标签周围的第一页上创建一个边框。

Any thoughts on how to do this? 有关如何做到这一点的任何想法? Is it possible to do it with regular JavaScript or jQuery? 是否可以使用常规JavaScript或jQuery来实现?

Thanks! 谢谢!

No , JavaScript is client-side and for this you would require the server to remember if the link was clicked, possibly by recording this in a database or session variable. ,JavaScript是客户端的,为此您需要服务器记住链接是否被单击,可能是通过将其记录在数据库或会话变量中。

That's of course if you're using the tradition model of a website, rather than loading pages already using pure JS. 当然,如果你使用网站的传统模型,而不是加载已经使用纯JS的页面。

It would be a pretty stupid way of doing it, but it is possible to do it client side. 这将是这样做的一个非常愚蠢的办法,但它可能这样做客户端。 I would seriously recommend to do it server-side though. 我会认真地建议在服务器端做它。

On page 2, link back to page 1 with a hash: 在第2页上,使用哈希链接返回第1页:

<a href="/page1#border">Go back to page one and add a border</a>

And on page 1, check if there's a hash: 在第1页,检查是否有哈希:

if (window.location.hash === '#border') {
    $('h2').addClass('withBorder');
}

I think if you are looking this kind of scenario you can achieve it with passing some hash to the url: 我想如果你正在寻找这种情况,你可以通过将一些哈希传递给url来实现它:

i passed a hash named 'second' in the second page url and put this script on second page 我在second page url passed a hash named 'second' ,并将此脚本放在第二页上

$(function(){
    var url=window.location;
    var hash = url.hash.substr(1);
    if(hash == "second"){
       $('h2').css('border','solid 1px red');
    }
});

Checkout this if helps. 如果有帮助,请查看此项。

Well there is a way you could do this with JavaScript, although it's tricky and server side is a LOT easier. 那么有一种方法可以用JavaScript做到这一点,虽然它很棘手,服务器端也很容易。 You would need to use some JavaScript to load different pages without refreshing the entire DOM. 您需要使用一些JavaScript来加载不同的页面而不刷新整个DOM。 I do this with something called pjax . 我用一种叫做pjax的东西来做这件事。 The way it works is to have each page act as a container to load all subsequent pages via ajax. 它的工作方式是让每个页面充当一个容器,通过ajax加载所有后续页面。 By not doing a full page reload, any style changes you make on one page get carried over to other pages (this dose not survive an actual browser refresh). 通过不执行完整页面重新加载,您在一个页面上进行的任何样式更改都会转移到其他页面(这种情况不会在实际的浏览器刷新后继续存在)。

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

相关问题 如何使用 JavaScript 或 JQuery 在页面加载时更改 Div 元素的样式宽度? - How do I change Style Width of Div Element on Page load using JavaScript or JQuery? 如何使用 javascript 内联更改另一个元素的样式? - How to change the style of another element inline using javascript? 使用JavaScript,如何根据另一个元素的CSS样式更改一个元素的CSS样式? - Using JavaScript, how do I change the CSS style of one element based on the CSS style of another element? 如何使用jQuery更改CSS元素的样式 - how to change style of a css element using jQuery 如何使用javascript引用另一个页面元素 - how to refer another page element using javascript 如何通过javascript或jQuery更改元素在另一个元素中的位置? - How to change the place of an element in another element by javascript or jQuery? 如何使用全局CSS或javascript来定位和更改元素样式? - How to target and change element style using global CSS or javascript? 如何更改“ <input> 动态使用Javascript元素? - How do I change the style of an “<input>” element dynamically using Javascript? 如何使用带有节点列表的Javascript更改其他元素的样式 - How to change style on other element using Javascript with nodelist 使用$(this)jQuery更改另一个元素 - using $(this) jquery to change another element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM