Sph*_*hvn 5 javascript cookies jquery
TLDR当单击链接时,我想分配一个带有仪器名称和所单击链接上的文本值的 cookie。
使用Jquery.1.4.2.min.js、Jquery.cookie.1.0.js
我试图在单击链接时创建一个 cookie(将始终链接到“page.html”)。
仪器名称
文本的值
到目前为止我正在尝试使用:
<script type="text/javascript" src="scripts/jquery-1.4.2-min.js"></script>
<script type="text/javascript" src="scripts/jquery.cookie-1.0.js"></script>
Run Code Online (Sandbox Code Playgroud)
链接1:
<a href="page.html">link1</a>
Run Code Online (Sandbox Code Playgroud)
链接2:
<a href="page.html">link2</a>
Run Code Online (Sandbox Code Playgroud)
脚本:
$('a[href=page.html]').click(function()
{
var name = 'instrument';
var value = $(this).text();
$.cookie(name, value, { expires: 365 });
});
Run Code Online (Sandbox Code Playgroud)
当我点击链接时,它只是加载链接并且没有设置 cookie。使用 firebug、firecookie、firequery 进行调试。没有找到仪器的 cookie 或类似的东西。加载我会点击
” <a href="page.html">projects</a>“但不是
” $.cookie(name, value, { expires: 365 });“根本没有。
甚至使用以下内容:(感谢Sarfraz)
$('a[href="page.html"]').click(function(e)
{
// prevent default action
e.preventDefault();
var name = 'instrument';
// get link text
var value = $(this).text();
// set cookie
$.cookie(name, value, { expires: 365 });
// now go to link's location
document.location.href = $(this).attr('href');
});
Run Code Online (Sandbox Code Playgroud)
仍然不会创建cookie。
如果我添加断点,$.cookie~即使单击其中一个链接,它也不会命中断点,也不会命中document.location.href~.
对于扎克:
预渲染 -
<script type="text/javascript" src="http://localhost/tree"></script>
渲染后 -
"<div class="dtree">
<div class="dTreeNode"><img alt="" src="Images/base.png" id="id0"><a onclick="javascript: d.s(0);" href="home" class="node" id="sd0">Home</a></div>"
Run Code Online (Sandbox Code Playgroud)
这有点骇人听闻,但对我有用。
$(document).ready( function() {
$('a.cookier').each( function() {
// Kill the link's link feature.
_href = $(this).attr('href');
$(this).attr("rel", _href).attr("href", "javascript:void(0);");
});
$('a.cookier').click(function() {
var name = 'instrument';
var value = $(this).text();
$.cookie(name, value, { expires: 365 });
// Go places
document.location.href = $(this).attr('rel');
});
});
Run Code Online (Sandbox Code Playgroud)
我的标记如下所示:
<a class="cookier" href="hrrrngh.html">Shazam!</a>
Run Code Online (Sandbox Code Playgroud)
我使用类似的方法来“取消链接”使用 AJAX 但必须正常降级的页面上的链接。
注意:您可能想要选择一个比“cookier”更优雅的类。
编辑:
您可以选择<a>其中的任何一个,选择不选择页面上其他内容的那个。
.dtree img + a
.dTreeNode > a
.dTreeNode a.node
.dTreeNode a[onclick=|javascript].node
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12027 次 |
| 最近记录: |