-1

这些是我的项目的 js 文件:

tablequerywrapper.js
gauge.min.js
Functions.js
colortip-1.0-jquery.js 
ObjectivesFunctions.js
jquery.bxslider.js

在:Functions.js,我有:

$('li div[id^="objective_option_conversion_points_"]').click(function () {
  alert('3');
}

$('#sortedPixels tbody').sortable({
    items: '> tr',
    forcePlaceholderSize: true,
    placeholder: 'must-have-class',
    start: function (event, ui) {
        // Build a placeholder cell that spans all the cells in the row
        var cellCount = 0;
        $('td, th', ui.helper).each(function () {
            // For each TD or TH try and get it's colspan attribute, and add that or 1 to the total
            var colspan = 1;
            var colspanAttr = $(this).attr('colspan');
            if (colspanAttr > 1) {
                colspan = colspanAttr;
            }
            cellCount += colspan;
        });

        // Add the placeholder UI - note that this is the item's content, so TD rather thanTR
        ui.placeholder.html('<td colspan="' + cellCount + '">&nbsp;</td>');
    }
}).disableSelection();

我运行它并得到:

Uncaught TypeError: Object #<Object> has no method 'sortable'

我读到这个错误是因为我需要JQUERY UI,所以我补充说:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

并且错误消失了。但是当我按下一个 id = "objective_option_conversion_points_1" 的 div 时,什么也没发生。

所以我尝试了:

$('li div[id^="objective_option_conversion_points_"]').on('click', function () {
    alert('3');
}

但什么也没发生。

所以我尝试了:

$('li div[id^="objective_option_conversion_points_"]').live('click', function () {
    alert('3');
}

但后来我又遇到了另一个错误:

Uncaught TypeError: Object [object Object] has no method 'live'

请问我该如何解决?

ps

在我的 jsfiddle 中,它有效:

http://jsfiddle.net/alonshmiel/adpFV/7/

任何帮助表示赞赏!

4

2 回答 2

2

live()已弃用,并且在您的 jQuery 版本中可能已被删除。

您可以为此目的使用委托。

但是选择器只是伤害了我的眼睛。你为什么不在#那里使用?

$('#objective_option_conversion_points_').on('click', function(){
    alert('3');
});
于 2013-09-20T19:08:44.783 回答
1

使用on()是处理事情的正确方法:

$( 'li' ).on( 'click', 'div[id^="objective ... "]', function () {
    // do something.
}

我不太确定选择 div 的语法,但第二个参数将侦听div[id^="obj ... "]创建的任何内容。

于 2013-09-20T19:11:23.877 回答