无法更改标签的 href 值
Cannot change href value of a tag
我正在尝试更改我的 href 值,但它似乎根本没有改变。 jQuery 和 javascript 这两种方法我都试过了,但都没有用。
<a type="button" class="view_me btn btn-primary" href="#">View Full</a>
$(document).on("click", ".click_row", function (e) {
e.preventDefault();
var _self = $(this);
userId = _self.data('id');
var newLink = '/users/view/' + userId //I'd like to eventually use this as new href link
$(".view_me").prop("href", "www.google.com"); // Tried testing with a simple webpage
// document.getElementById('view_test').href = "www.google.com"; // Tried this as well, doesn't work
console.log(newLink)
});
任何建议都会有所帮助。我正在使用 jQuery 3.2.1
有一些问题阻碍了预期的行为。首先,你的 click
函数没有被调用,因为它正在寻找 click_row
class;我相信您的意思可能是 view_me
class。
其次,您需要将 https://
添加到 URL 的开头以使其正确重定向。最后,您需要在具有 class view_me
的代码中设置 link 的 href
属性 ,而不是 id view_me
.
编辑:另外,删除 e.preventDefault()
否则您只能在新选项卡中打开 link。
以下 JS 代码修复了这些问题并产生了所需的行为:
$(document).on("click", ".view_me", function (e) {
//e.preventDefault();
var _self = $(this);
userId = _self.data('id');
var newLink = '/users/view/' + userId
$(".view_me").prop("href", "https://www.google.com");
console.log(newLink);
});
工作 JSFiddle:https://jsfiddle.net/2m0297j1/
我正在尝试更改我的 href 值,但它似乎根本没有改变。 jQuery 和 javascript 这两种方法我都试过了,但都没有用。
<a type="button" class="view_me btn btn-primary" href="#">View Full</a>
$(document).on("click", ".click_row", function (e) {
e.preventDefault();
var _self = $(this);
userId = _self.data('id');
var newLink = '/users/view/' + userId //I'd like to eventually use this as new href link
$(".view_me").prop("href", "www.google.com"); // Tried testing with a simple webpage
// document.getElementById('view_test').href = "www.google.com"; // Tried this as well, doesn't work
console.log(newLink)
});
任何建议都会有所帮助。我正在使用 jQuery 3.2.1
有一些问题阻碍了预期的行为。首先,你的 click
函数没有被调用,因为它正在寻找 click_row
class;我相信您的意思可能是 view_me
class。
其次,您需要将 https://
添加到 URL 的开头以使其正确重定向。最后,您需要在具有 class view_me
的代码中设置 link 的 href
属性 ,而不是 id view_me
.
编辑:另外,删除 e.preventDefault()
否则您只能在新选项卡中打开 link。
以下 JS 代码修复了这些问题并产生了所需的行为:
$(document).on("click", ".view_me", function (e) {
//e.preventDefault();
var _self = $(this);
userId = _self.data('id');
var newLink = '/users/view/' + userId
$(".view_me").prop("href", "https://www.google.com");
console.log(newLink);
});
工作 JSFiddle:https://jsfiddle.net/2m0297j1/