简体   繁体   English

如何使用Javascript或HTML保存文章ID?

[英]How do I save article ids in Javascript or HTML?

I have a list of links in the web-page and users can click and comment on them. 我在网页中有一个链接列表,用户可以单击并评论它们。 I load the links via JSON and each link is uniquely identified by an id. 我通过JSON加载链接,每个链接由一个ID唯一标识。

My question is how do I know which link has been clicked? 我的问题是如何知道单击了哪个链接? Even though I have an id, where do I store it(If Javascript object, how?)? 即使我有一个id,我也应该将其存储在哪里(如果是Javascript对象,怎么办?)? I guess I cannot have it as a tag attribute because users may change it. 我想我不能将其作为标签属性,因为用户可能会对其进行更改。

It sounds like you want to be able to create a link to a page with a specific ID, without actually displaying the ID? 听起来您希望能够创建一个具有特定ID的页面的链接,而无需实际显示ID? If this is the case, I think you'll face difficulties doing that in a simple way, as javascript client-side and the user will most likely be able to alter the id anyway. 如果是这种情况,我认为您将很难以简单的方式完成此操作,因为javascript客户端和用户很可能无论如何都可以更改id。 I'd recommend keeping the id in the link ( <a href="url.com/post/42> , for example) and rather, when the page is loaded, in some way or another, check if the provided URL is actually a valid, existing ID. 我建议将id保留在链接中(例如<a href="url.com/post/42> ),并且,当以某种方式加载页面时,请检查提供的URL是否确实有效的现有ID。

You can use data attributes which stays hidden from end user. 您可以使用对最终用户隐藏的数据属性。

Eg: <a id="link1" data-myuniqueid="XXXXX"> Link1 </a> 例如: <a id="link1" data-myuniqueid="XXXXX"> Link1 </a>

Using Javascript, you can access/modify data values 使用Javascript,您可以访问/修改数据值

var ele = document.getElementById("link1");
var uid = ele.getAttribute('data-myuniqueid'); // For compatibility
var uid = ele.dataset.myuniqueid;  // HTML5 way - does not work in old browsers

if you need more explaination for using custom data attributes, this might help. 如果您需要更多有关使用自定义数据属性的说明, 可能会有所帮助。

-- EDIT -- -编辑-

I guess above answer will not be sufficient to address your problem. 我想以上答案不足以解决您的问题。 You can store objects in data attributes or try something like this - 您可以将对象存储在数据属性中,或尝试类似的操作-

var ele = document.getElementById("link1");
ele.myuniqueid = "XXXXXX"

myuniqueid will not be visible to user even in source. myuniqueid即使在源中也对用户不可见。

You can add it as a tag attribute - anything that is send to the browsers, users can change. 您可以将其添加为标记属性-发送到浏览器的任何内容,用户都可以更改。 If a user wants to change the id - let him do it. 如果用户想要更改ID,请让他这样做。 He can also click on the article that he wants to comment on and do that as well. 他还可以单击要评论的文章,​​也可以这样做。

You can use the sessionStorage in HTML5 suppoerted by all the browsers. 您可以在所有浏览器支持的HTML5中使用sessionStorage。 It can also retain if the page gets reloaded 如果页面被重新加载,它也可以保留

//for setting value 
sessionStorage.LinkClicked= "4";

//to get the value
var temp = sessionStorage.LinkClicked;

Here is the link for more info http://www.w3schools.com/html/html5_webstorage.asp 这是更多信息的链接http://www.w3schools.com/html/html5_webstorage.asp

Hope, it may help you. 希望对您有帮助。 Have anice day. 祝你今天愉快。 :) :)

for storing data on the client side. 用于在客户端存储数据。 if your not fussy about cross browser compadablility, you could go with a html5 soultion such as local storage. 如果您对跨浏览器的兼容性不太挑剔,则可以使用html5灵魂,例如本地存储。

http://html5sql.com/ http://html5sql.com/

http://diveintohtml5.info/storage.html http://diveintohtml5.info/storage.html

if your looking to get the id from a element and your using jQuery you can uses this in your event handler 如果您希望从元素中获取ID并使用jQuery,则可以在事件处理程序中使用它

$().attr('id'); 

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

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