嗨,我正在尝试将表单放置在具有特定URL的图像下方,但是它不起作用。我不确定
由于我是JQuery新手,所以我做错了什么。有人会知道这是怎么回事吗?

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript">
function main(){
    var imageURL = getImageURL();
    $("img[src=imageURL]");
    $("img[src='Your URL']").append("<input type="radio" name="geo" value="geolocation">Use Geolocation? <br> Additional Information About the Image: <input type="comment" name="cmnt">");

}

function getImageURL(){
var url = "http://www.w3schools.com/images/pulpit.jpg";
return url;
}
</script>
</head>
<body onload="main()">
<b> Page </b>
<img src="http://www.w3schools.com/images/pulpit.jpg">

</body>
</html>

最佳答案

几个问题。您不能使用变量imageURL不能直接放在src选择器中。您需要将其与+串联在一起。然后,不是使用.append()向给定节点的子节点添加元素,而是需要使用.after()将元素作为兄弟节点添加到选择器。

您应该添加一个嵌套在其中的<input>而不是一个单独的<form>

function main(){
    var imageURL = getImageURL();
    // Concatenate the imageURL variable into the $() selector.
    // Put the <input> inside a <form> if it needs to function as a form element.
    $("img[src='" + imageURL + "']").after("<form><input type="radio" name="geo" value="geolocation">Use Geolocation? <br> Additional Information About the Image: <input type="comment" name="cmnt"></form>");

}

10-06 15:04