jquery
  • jquery-mobile
  • 2013-04-02 94 views 0 likes 
    0

    我有一个div,我想将一段代码添加到onclick中。问题是代码不起作用。我认为这事做的onclick和我使用“/”使用jquery将链接附加到div

    这里是jQuery的注释掉的尝试:

    $("#content_type_nav").html("<a id='add_content_container' onClick=/"window.location.href='create.php';/"><img id='add_content_icon' src='Site_Images/add_column.PNG'/></a>"); 
    

    下面是HTML:

    <div data-role="footer" id="content_type_nav" data-position="fixed"> 
         //I want the link to be inserted here 
         <h1>Show me my...</h1> 
        </div> 
    
    +0

    和你遇到的问题是......? – j08691

    +0

    而且你不应该使用内联“onclick”...... –

    +0

    链接不会出现在点击中。上面的代码是否正确?我想用/附近的“? –

    回答

    3

    既然你已经使用jQuery,你应该委派onclick事件。
    并使用prepend()作为第一个孩子插入。

    var html = "<a id='add_content_container'><img id='add_content_icon' src='Site_Images/add_column.PNG'/></a>"; 
    
        $("#content_type_nav").prepend(html); 
        $("#content_type_nav").on('click', 'a#add_content_container', function(){ 
          window.location.href='create.php'; 
        }); 
    
    +0

    谢谢你的帮助 –

    2

    好像你正在替换该div的内容。如果要追加到该div,要使用append()方法,而不是HTML()方法

    +0

    我更新了问题的HTML。我也显示我试图让链接出现。 –

    2

    从您的代码,试试这个:

    $("#content_type_nav").append("<a id='add_content_container' onClick=window.location.href='create.php;'/><img id='add_content_icon' src='Site_Images/add_column.PNG'/></a>"); 
    
    +0

    我试着这与prepend而不是append。但链接显示,但它不会工作。无论如何,谢谢! –

    相关问题