简体   繁体   English

Javascript 文件修改一个 html 文件,它不是从中调用的? Jquery 选择器

[英]Javascript file modifying an html file that it is not called from? Jquery Selectors

Is its possible to have a javascript file that is aware of two different HTML files?是否有可能有一个 javascript 文件知道两个不同的 HTML 文件? And how would I do this?我该怎么做?

I would like to be able to have two pages.我希望能够有两页。 index.html and pictures.html.索引.html 和图片.html。 I have an index.js that changes the display properties of index.html (it puts data based on people into tables and makes it look nice).我有一个 index.js 可以更改 index.html 的显示属性(它将基于人的数据放入表中并使其看起来不错)。 I would like this current index.js file also to be able to edit the pictures.html file and change information there.我希望当前的 index.js 文件也能够编辑 pictures.html 文件并更改那里的信息。 index.html would link to pictures.html to display pictures of a person (based on the persons name I have them saved smith1.jpg, smith2.jpg, reagan2.jpg, ect). index.html 将链接到 pictures.html 以显示一个人的图片(根据人名,我将它们保存为 smith1.jpg、smith2.jpg、reagan2.jpg 等)。 Is there anyway that this javascript file could get DOM elements based on their id or class of the second file (pictures.html) even though it "lives in" index.html?无论如何,这个 javascript 文件是否可以根据第二个文件(pictures.html)的 id 或 class 获取 DOM 元素,即使它“存在于”index.html 中? When i say lives in it is called at the top of the index.html page.当我说住在里面时,它在 index.html 页面的顶部被调用。

thanks谢谢

You'll need to explore server-side programming to accomplish your goal.您需要探索服务器端编程才能实现您的目标。 http://en.wikipedia.org/wiki/Server-side_scripting http://en.wikipedia.org/wiki/Server-side_scripting

...Or you could write a client-side application in which "pages" are separate views of one actual page or are generated from backing data structures. ...或者您可以编写一个客户端应用程序,其中“页面”是一个实际页面的单独视图或从支持数据结构生成。 If you want persistance of what is created/edited, you'll still need server-side programming.如果您想要持久保存创建/编辑的内容,您仍然需要服务器端编程。

A script can access elements on another page if it was loaded in some way of connection.如果脚本以某种连接方式加载,则它可以访问另一个页面上的元素。

For example, if you make a popup using var popup = window.open() , the return value will contain a reference to the opened popup and this allows access to elements within the popup.例如,如果您使用var popup = window.open()创建弹出窗口,则返回值将包含对打开的弹出窗口的引用,这允许访问弹出窗口中的元素。 Eg popup.document.getElementById('something') .例如popup.document.getElementById('something') Pages loaded within frames, iframes and such have similar ways of access.在 frames、iframe 等中加载的页面具有类似的访问方式。

So yes, if your page loads the second page its script can work there as well.所以是的,如果你的页面加载第二页,它的脚本也可以在那里工作。 I suggest avoiding this beyond opening and closing popups from a script though;我建议除了从脚本打开和关闭弹出窗口之外避免这种情况; a script should stay inside the box of its page and if it needs to do larger operations on another page, that usually means that you need to change your code architecture a bit.一个脚本应该留在它的页面框内,如果它需要在另一个页面上做更大的操作,这通常意味着你需要稍微改变你的代码架构。

You can use the html5 (group of technologies) postmessage api as well.. This allows you to send messages to another page, and in that page you define an event handler that knows how to handle the message.您也可以使用 html5(技术组)postmessage api。这允许您将消息发送到另一个页面,并在该页面中定义一个知道如何处理消息的事件处理程序。

This also works across domains.这也适用于跨域。

Here is a blog with an example I just randomly found via google: http://robertnyman.com/2010/03/18/postmessage-in-html5-to-send-messages-between-windows-and-iframes/这是一个博客,其中包含我刚刚通过谷歌随机找到的示例: http://robertnyman.com/2010/03/18/postmessage-in-html5-to-send-messages-between-windows-and-iframes/

Not possible on the client side if editing the actual HTML file is your goal.如果您的目标是编辑实际的 HTML 文件,则在客户端不可能。 If getting pictures to show up depending on stuff a user does on another page is all you care about then there are lots of options.如果根据用户在另一个页面上所做的事情让图片显示是您所关心的,那么有很多选择。

  • You can pass small sets of data like stuff the user entered into tables via cookies for accessing the right sets of image files in a pre-established scheme.您可以通过 cookies 将用户输入表格的小数据集传递给表格,以便在预先建立的方案中访问正确的图像文件集。 This would actually persist until a user cleared out cookies.这实际上会持续到用户清除 cookies 为止。

  • You could wrap both pages in same-domain iframe elements with the parent element containing just the JS.您可以将两个页面包装在相同域的 iframe 元素中,父元素仅包含 JS。 This would allow you to persist data between pages and react to iframe load events but like everything in client-side JS, it's all gone when you reload the page.这将允许您在页面之间保留数据并对 iframe 加载事件做出反应,但就像客户端 JS 中的所有内容一样,当您重新加载页面时,它都消失了。

  • Newer browsers have working file access objects that aren't total security nightmares.较新的浏览器具有工作文件访问对象,这并不是完全的安全噩梦。 These are new and non-standard enough that it would take some doing to make it work for multiple browsers.这些都是新的和非标准的,需要做一些工作才能使其适用于多种浏览器。 This could be used to save files containing info that the user would probably have to be prompted to upload when they return to the site.这可用于保存包含用户返回站点时可能需要上传的信息的文件。

  • If the data's not sensitive you could get creative and use another service to stash collections of data.如果数据不敏感,您可以发挥创意并使用其他服务来存储 collections 数据。 Use a twitter API to tweet data to some publicly visible page of a twitter account (check the Terms of Service if you're doing anything more than an isolated class project here).使用 twitter API 将数据推到 twitter 帐户的某个公开可见页面(如果您在此处做的不仅仅是孤立的 class 项目,请查看服务条款)。 Then do an Ajax get request on whatever URL it's publicly visible at and parse the HTML for your twitter data.然后对公开可见的任何 URL 执行 Ajax 获取请求,并解析 HTML 以获取您的 twitter 数据。

Other stuff I'd look into: dataURIs, html5 local storage.我会研究的其他内容:dataURI、html5 本地存储。

Note: None of these are approaches I would seriously consider for a professional site where the data was expected to be persistent or in any way secure regardless of where a user accesses it from.注意:这些都不是我会认真考虑的专业网站的方法,在专业网站上,无论用户从哪里访问数据,数据都应该是持久的或以任何方式安全。

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

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