简体   繁体   English

如何动态地将jQuery移动弹出效果添加到链接

[英]how to dynamically add jquery mobile popup effect to links

In jquery mobile, I dynamically add a tags that is supposed to open a popup like in this example below. 在jquery mobile中,我动态添加a标签, a标签应打开一个弹出窗口,如下面的示例所示。 But since it is dynamically added, the jquery mobile effects don't affect it. 但是由于它是动态添加的,因此jQuery移动效果不会对其产生影响。 How can I get it to work like this? 我怎样才能使它像这样工作?

Thanks 谢谢

<a href="#popupMenu" data-rel="popup" data-transition="slideup" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-gear ui-btn-icon-left ui-btn-a">Actions...</a>
<div data-role="popup" id="popupMenu" data-theme="b">
        <ul data-role="listview" data-inset="true" style="min-width:210px;">
            <li data-role="list-divider">Choose an action</li>
            <li><a href="#">View details</a></li>
            <li><a href="#">Edit</a></li>
            <li><a href="#">Disable</a></li>
            <li><a href="#">Delete</a></li>
        </ul>
</div>

When you append any html into a page after the page's initial load, you need to reapply any of the jquery functions in order for them to work when the event occurs 在页面的初始加载后将任何html附加到页面中时,您需要重新应用任何jquery函数,以便它们在事件发生时起作用

Example... 例...

if you currently have something like this: 如果您当前有这样的事情:

    <script type="text/javascript">
    $(document).ready(function () {
      $('a').on('click', function () {
            //something
            return false;
        });
    });
    </script>

This will do //something whenever the user clicks on any < a > link. 每当用户单击任何<a>链接时,这都将执行//某些操作。 Now that you are loading the new < a > links in after the document is ready, the code above will not apply to the new code as it was not on the page when the javascript applied the above code. 现在您准备好在文档准备好后加载新的<a>链接,以上代码将不适用于新代码,因为当javascript应用上述代码时,该代码不在页面上。

To fix this, you need to run the function that does the //something again after the new < a > has been loaded. 要解决此问题,您需要运行在新<a>加载后再次执行//动作的函数。

    <script type="text/javascript">
    $(document).ready(function () {

            somethingFunction();

        });
    });

    //this is where we put the code to apply an event, it is now recallable later on.
    function somethingFunction(){
    $('a').on('click', function () {
            //something
            return false;
        });
    }
    </script>

Assuming that you are pulling in the new < a > html via an ajax query, you would need to call the somethingFunction() after the ajax query is successful 假设您要通过ajax查询引入新的<a> html,则在ajax查询成功后,您需要调用somethingFunction()

EG 例如

$.ajax({
type: "POST",
url: "/action" ,
dataType: 'text',
success: function(data){
    $('.popups').html(data);
    somethingFunction(); // THIS IS WHERE IT APPLIES THE EVENT TO YOUR NEW HTML.
}
});

If I understood correctly, you want to add buttons dinamically, resulting in the style proposed in your example: 如果我理解正确,那么您想动态地添加按钮,从而得到示例中建议的样式:

class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-gear ui-btn-icon-left ui-btn-a"

To apply jQuery Mobile enhancements after appending some HTML you have to call the widget creation method: .button(); 要在附加一些HTML之后应用jQuery Mobile增强功能,您必须调用小部件创建方法: .button(); in this case (can be .checkboxradio(); , .listview(); , etc). 在这种情况下(可以是.checkboxradio(); ,. .listview();等)。

I put toghether a JSFiddle which demonstrates dynamically creating a button calling .button() and also applying hardcoded classes (which I think it's not a good thing to do). 我一起写了一个JSFiddle ,它演示了动态创建一个调用.button()的按钮并应用了硬编码的类(我认为这样做不是一件好事)。

There is a demo available in jquery mobile documents Dynamically create popup jQuery移动文档中有一个演示可动态创建弹出窗口

Following i write a simple html tags in javascript then append in to jquery mobile pages. 接下来,我在javascript中编写了一个简单的html标记,然后追加到jquery移动页面中。

html: 的HTML:

 <div data-role="page">
    <div data-role="content" id="forpopup">
    </div>
<div>

Here is the Fiddle Demo . 这是小提琴演示

i hope this will be helpful. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM