我根据其他StackOverflow问题/答案设置了一些Jquery.这个脚本的目的是它使整个div成为基于该div内的任何href标记的链接.
这工作正常,但我需要将其设置为_blank以在新选项卡中打开.我已经尝试了以下没有运气.
$(document).ready(function() {
$(".product-item").click(function() {
$(this).target = "_blank";
window.location = $(this).find("a").attr("href");
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
编辑
感谢您的帮助,但这些答案都没有实际效果.如果任何人都可以粘贴实际工作的完整代码,而不是没有测试的小片段,如果它工作.谢谢.
编辑2
感谢kristinalim,提供完整的工作解决方案.
kri*_*lim 23
在页面上设置链接需要@Ravi和@ncksllvn的答案组合:
// Find link in $(".product-item") and set "target" attribute to "_blank".
$(this).find("a").attr("target", "_blank");
Run Code Online (Sandbox Code Playgroud)
要在另一个窗口中打开页面,请参阅此问题:jQuery单击_blank并查看此参考以window.open获取自定义选项.
更新:
你需要一些东西:
$(document).ready(function() {
$(".product-item").click(function() {
var productLink = $(this).find("a");
productLink.attr("target", "_blank");
window.open(productLink.attr("href"));
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
注意用法.attr():
$element.attr("attribute_name") // Get value of attribute.
$element.attr("attribute_name", attribute_value) // Set value of attribute.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
103493 次 |
| 最近记录: |