簡體   English   中英

$(a,this).attr('href')返回undefined

[英]$(a,this).attr('href') returns undefined

我使用ajax從mysql數據庫加載一些數據...我的問題是,它獲取我想加載的數據的id,我已將HREF值設置為id ...所以一個例子是:

`<a href="16" title="View Story: ssasds">ssasds</a>`,

16是我需要的id值...我的代碼是:

$('.artefact').click(function()
                {

                var storyId = $('a',this).attr('href');
                console.log(storyId);
                               }

當我檢查控制台(firebug)它只是說未定義。 請嘗試幫忙,因為我已經嘗試過獲取數據的其他方法但是變得混亂。

謝謝

好像.artefact有兩個a秒。 基於此:

$('.artefact').click(function () {
    var storyId = $('a', this).filter("[href]").attr('href');
    console.log(storyId);
});

編輯

再想一想,這看起來更干凈:

$('.artefact').click(function () {
    var storyId = $(this).find("a[href]").attr('href');
    console.log(storyId);
});

.artefact是鏈接嗎? 如果是,為什么要使用$('a', this)而不是$(this)

div元素中有2個鏈接(查看您提供的代碼)。 所以要獲得href(如果該鏈接始終是div中的最后一個,你應該使用:

$('.artefact').click(function() {
  var storyId = $('a', this).last().attr('href');
  console.log(storyId);
});

正如其他人所說,你也缺少了腸胃外症。

嘗試這個:

var storyId = $(this).attr('href');

你需要使用$(this).attr('href')

是的,關閉括號是錯誤的http://jsfiddle.net/h7HuJ/1/我使用了你給定的HTML代碼。

您在DIV中有兩個錨點,因此您需要指定要選擇的錨點。 我用了你的班級名字。

@Neil Stewart:請參閱 - http://jsfiddle.net/Chby2/

您沒有將artefact類添加到鏈接中,並且您的jQuery代碼也缺少右括號:) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM