使用jquery将链接附加到div

使用jquery将链接附加到div,jquery,jquery-mobile,Jquery,Jquery Mobile,我有一个div,我想在onclick中附加一段代码。问题是代码不起作用。我认为这与onclick和我用“/”注释掉的尝试有关 以下是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'/&

我有一个div,我想在onclick中附加一段代码。问题是代码不起作用。我认为这与onclick和我用“/”注释掉的尝试有关

以下是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(“”);
以下是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>

//我想在这里插入链接
让我看看我的。。。

您似乎正在替换该div的内容。如果要附加到该div,请在代码中使用append()方法而不是html()方法,请尝试以下操作:

$("#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>");
$(“#内容#类型_导航”)。追加(“”);

因为您已经在使用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';
    });
var html=”“;
$(“#内容(类型)导航”).prepend(html);
$(“#content_type_nav”)。在('click','a#add#content_container',function()上{
window.location.href='create.php';
});

您遇到的问题是…?而且无论如何都不应该使用内联“onclick”…单击时也不会显示链接。上面的代码正确吗?我是否应该使用“/靠近”?我用html更新了问题。我还显示了我试图获取链接的位置。我用prepend而不是append尝试了此操作。但是链接出现了,但它不起作用。不过还是谢谢!