我想要,具有此腳本:如何訪問jQuery .on()的「第一個」選擇器?
$('[data-foo]').on('click', '.button-action', function() {
// Show alert on $('.button-action', this).
// Alert message: Lorem ipsum {$('.foo-id', this).text()}
});
而且我有多個元素這樣的,動態生成的:
<div data-foo="{id}>
<div class="foo-id">
Lorem Value
</div>
<button class="button-action">Click to show the id of this data-foo</button>
</div>
換句話說,我想訪問$('[data-foo]')
this
按鈕我點擊。
它們是如何生成的?你可能會添加一個'id'到每個按鈕,這將徹底解決這個問題? –
它們是用一個隨機的'data-foo'值和'Lorem Value'生成的,我想要的只是一個通用的'ID',在這種情況下是'data-foo'並且基於該ID,瞄準事件。 – Mithc