【问题标题】:unable to select jQuery dynamic element无法选择 jQuery 动态元素
【发布时间】:2014-03-21 04:05:43
【问题描述】:

选择,我的意思是select()

我的代码是这样的:

    $('.show-embed-link').unbind('click');
    $(".show-embed-link").click(function(e){
        var id = $(this).attr("rel");
        e.preventDefault();
        showEmbed(id);
        setTimeout(function() {
            $("#general_message").focus();
        }, 100);
    });

.show-embed-link 不是动态元素。它是一个<a> 元素。

showEmbed 将生成一个动态元素。

function showEmbed(id) {
        var message = '<iframe width="1000" height="800" src="//storyzer.com/stories/'+id+'" frameborder="0" allowfullscreen></iframe>';
        message = HtmlEncode(message);
        showOverlayForGeneral(message, "Embed work", {'spinner': false, 'extraheight': 90, 'showclose': true});
 }

showOverlayForGeneral负责生成带有消息的动态元素。

function showOverlayForGeneral(message, title, options) {
    options = (typeof options === "undefined") ? {} : options;
    var defaultOptions = {
        "message": "",
        "extraheight": 150,
        "spinner": true,
        "showclose": false
    };

       // code removed because not relevant to this situation...

    $('#general_message').unbind('focus');
    $('#general_message').focus(function () {
        $('#general_message').select().mouseup(function (e) {
            e.preventDefault();
            $(this).unbind("mouseup");
        });
    });
}

focus 代码取自 https://stackoverflow.com/a/3380493

目前,我的 div html 未被选中。如何判断 select 事件是否正在发生?

【问题讨论】:

  • 看看.on()api.jquery.com/on
  • 我应该如何使用它?
  • 你已经提供了你尝试过的东西。

标签: javascript jquery html select


【解决方案1】:

试试这个

$(document).ready(function(){
     $('#general_message').unbind('focus');
        $('#general_message').focus(function () {
            $('#general_message').select().click(function (e) {
                e.preventDefault();
                $(this).unbind("click");
            });
        });
})

【讨论】:

  • 为什么要把mouseup改成点击?
  • 我认为你想关注 div 部分,所以当你点击那个 div 时它应该专注于你的 div 所以我只需将 mouseup 替换为点击功能。@KimStacks
  • 我不想点击 div。我希望它通过 javascript 聚焦。请参阅我的第一个代码块。我用过 .focus()
  • 焦点功能在 div 点击函数内部,那么焦点函数如何在不点击该 div 的情况下工作?我编辑了我的答案,看到了。 @KimStacks
猜你喜欢
  • 2017-08-02
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多