Javascript jquery从data href获取数据
我尝试使用jQuery将表行设置为链接,如下所示:Javascript jquery从data href获取数据,javascript,jquery,html,Javascript,Jquery,Html,我尝试使用jQuery将表行设置为链接,如下所示: jQuery(document).ready(function() { $(".clickable-row").click(function() { window.location.href = $(this).data('href'); }); }); 在我的html文件中,我有以下内容: <tr class='clickable-row' data-href='my-page.com/user/123
jQuery(document).ready(function() {
$(".clickable-row").click(function() {
window.location.href = $(this).data('href');
});
});
在我的html文件中,我有以下内容:
<tr class='clickable-row' data-href='my-page.com/user/123'>
<td><span class="glyphicon glyphicon-user"></span></td>
<td>{{ member.getFornamn }} {{ member.getEfternamn }}</td>
<td>{{ member.getEmail }}</td>
<td>{{ member.getTel1 }}</td>
<td>{{ member.getPostadress }}</td>
<td>{{ member.getPostnr }}</td>
</tr>
但是,当我更改到这个控制台日志时,我甚至都无法识别我的单击
jQuery(document).ready(function() {
$(".clickable-row").click(function() {
window.location.href = $(this).data('href');
});
});
我这样做:
jQuery(document).ready(function() {
$(".clickable-row").click(function() {
thisdata = $(this).attr('data-href');
console.log(thisdata);
window.location.href = thisdata;
});
});
控制台给了我正确的答案
我注意到我的实际桌子是这样的:
因此,通过删除URL://它现在就可以工作了
谢谢你的帮助 data href
属性是否动态设置?请尝试$(this.attr('data-href');和$('body')。在('click','clickable row',function(){..您的代码在这里..});控制台日志$(this).data('href')
,以确保它是您认为应该的。另外,也许你一开始就需要http://
。@mohammad:现在不需要,但在它工作时就需要了。实际上无法复制,我打赌是.data('href')
造成的。使用jQuery的data()。根据我的经验,使用.attr('data-href')
进行获取要可靠得多:它按照您的预期处理页面加载后注入的元素(即AJAX),并且还允许CSS规则。
jQuery(document).ready(function() {
$(".clickable-row").click(function() {
thisdata = $(this).attr('data-href');
console.log(thisdata);
window.location.href = thisdata;
});
});