Javascript 无法从用户创建的函数重写单击事件以定位标记
我不知道如何解释我的情况,如果你能解释得更清楚,请编辑我的问题 我可以从window.load或document.ready覆盖锚定标记的单击事件,但当我从用户创建的函数尝试时,问题出现了 我的JavaScript代码是:Javascript 无法从用户创建的函数重写单击事件以定位标记,javascript,jquery,html,css,ajax,Javascript,Jquery,Html,Css,Ajax,我不知道如何解释我的情况,如果你能解释得更清楚,请编辑我的问题 我可以从window.load或document.ready覆盖锚定标记的单击事件,但当我从用户创建的函数尝试时,问题出现了 我的JavaScript代码是: $('#up').click(function(){ $('#Vote').html("<div id=\"aniDiv\" style=\"background:green;height:25px;width:80px;position:absolute;float:
$('#up').click(function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:green;height:25px;width:80px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You upvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
attachUnvote();
});
$('#down').click(function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:red;height:25px;width:50px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You downvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
attachUnvote()
});
function attachUnvote()
{
$('#unvote').click(function(){
$('#Vote').html("<a style='text-decoration:none;background-color:green;padding:3px;color:white;' id='up' href='#'>Up</a><a style='text-decoration:none;background-color:red;padding:3px;color:white' id='down' href='#'>Down</a>");
});
}
$('#up')。单击(函数(){
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
投票权();
});
$('#down')。单击(函数(){
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
投票
});
函数名为
{
$('#unnote')。单击(函数(){
$('#Vote').html(“”);
});
}
我的目标是,在attachUnvote()
函数设置了#Vote元素的HTML之后,它应该调用attachUnvote()
函数上方的代码。我试图将上面的代码封装在另一个函数中,并在attachUnvote()
中调用该函数,但它不起作用
您可以在此处找到实时演示、HTML和CSS:您可以通过使用投票包装器上的委托事件处理程序来避免附加和取消附加这些事件处理程序 更新后的代码如下所示:
$('#Vote').on('click','#up',function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:green;height:25px;width:80px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You upvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
});
$('#Vote').on('click','#down',function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:red;height:25px;width:50px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You downvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
});
$('#Vote').on('click','#unvote',function(){
$('#Vote').html("<a style='text-decoration:none;background-color:green;padding:3px;color:white;' id='up' href='#'>Up</a><a style='text-decoration:none;background-color:red;padding:3px;color:white' id='down' href='#'>Down</a>");
});
$('Vote')。在('click','up',函数()上{
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
});
$(“#投票”)。在('click','#down',函数()上{
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
});
$(“#投票”)。在('click','#unnote',函数()上{
$('#Vote').html(“”);
});
通过在投票包装器上使用委托事件处理程序,可以避免附加和取消附加这些事件处理程序
更新后的代码如下所示:
$('#Vote').on('click','#up',function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:green;height:25px;width:80px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You upvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
});
$('#Vote').on('click','#down',function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:red;height:25px;width:50px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You downvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
});
$('#Vote').on('click','#unvote',function(){
$('#Vote').html("<a style='text-decoration:none;background-color:green;padding:3px;color:white;' id='up' href='#'>Up</a><a style='text-decoration:none;background-color:red;padding:3px;color:white' id='down' href='#'>Down</a>");
});
$('Vote')。在('click','up',函数()上{
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
});
$(“#投票”)。在('click','#down',函数()上{
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
});
$(“#投票”)。在('click','#unnote',函数()上{
$('#Vote').html(“”);
});
通过在投票包装器上使用委托事件处理程序,可以避免附加和取消附加这些事件处理程序
更新后的代码如下所示:
$('#Vote').on('click','#up',function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:green;height:25px;width:80px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You upvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
});
$('#Vote').on('click','#down',function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:red;height:25px;width:50px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You downvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
});
$('#Vote').on('click','#unvote',function(){
$('#Vote').html("<a style='text-decoration:none;background-color:green;padding:3px;color:white;' id='up' href='#'>Up</a><a style='text-decoration:none;background-color:red;padding:3px;color:white' id='down' href='#'>Down</a>");
});
$('Vote')。在('click','up',函数()上{
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
});
$(“#投票”)。在('click','#down',函数()上{
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
});
$(“#投票”)。在('click','#unnote',函数()上{
$('#Vote').html(“”);
});
通过在投票包装器上使用委托事件处理程序,可以避免附加和取消附加这些事件处理程序
更新后的代码如下所示:
$('#Vote').on('click','#up',function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:green;height:25px;width:80px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You upvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
});
$('#Vote').on('click','#down',function(){
$('#Vote').html("<div id=\"aniDiv\" style=\"background:red;height:25px;width:50px;position:absolute;float:left\" class=\"base\"></div><div class=\"overlay\"><a style=\"text-decoration:none;color:black\" id='unvote' href=\"#\">You downvoted. Click to take vote back.</a></div>");
$("#aniDiv").animate({width:"10px"});
$("#Vote").width("350px");
});
$('#Vote').on('click','#unvote',function(){
$('#Vote').html("<a style='text-decoration:none;background-color:green;padding:3px;color:white;' id='up' href='#'>Up</a><a style='text-decoration:none;background-color:red;padding:3px;color:white' id='down' href='#'>Down</a>");
});
$('Vote')。在('click','up',函数()上{
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
});
$(“#投票”)。在('click','#down',函数()上{
$('#Vote').html(“”);
$(“#aniDiv”).animate({width:“10px”});
美元(“#投票”)。宽度(“350px”);
});
$(“#投票”)。在('click','#unnote',函数()上{
$('#Vote').html(“”);
});
活动委派是一种方式
我可能会在这里吹毛求疵,但这也让编码变得有趣,而不是陷入一片混乱。有一些能让你的生活更轻松的建议
避免内联样式。改用类。(将有助于分离关注点)
使用函数避免重复代码
HTML
<div id='Vote' style="margin:0">
<a class="p-small bg-green" id='up' href="#">Up</a>
<a class="p-small bg-red" id='down' href="#">Down</a>
</div>
JAVASCRIPT
var $defaultDiv = "<a class='p-small bg-green' id='up' href='#'>Up</a>"
+ "<a class='p-small bg-red' id='down' href='#'>Down</a>",
$animatedDiv = "<div id='aniDiv' class='base {{bg}}'></div>"
+ "<div class='overlay'>"
+ "<a class='black' id='unvote' href='#'>You {{vote}}. Click to take vote back.</a></div>";
$('#Vote').on('click', '#up', function() {
animateDiv("bg-green", true);
});
$('#Vote').on('click', '#down', function() {
animateDiv("bg-red", false);
});
$('#Vote').on('click', '#unvote', function() {
$('#Vote').html($defaultDiv);
});
function animateDiv(bgColor, upVoted) {
$('#Vote').html($animatedDiv.replace("{{bg}}", bgColor)
.replace("{{vote}}", upVoted ? "upvoted" : "downvoted"));
$("#aniDiv").animate({
width: "10px"
});
$("#Vote").width("350px");
var$defaultDiv=“”
+ "",
$animatedDiv=“”
+ ""
+ "";
$(“#投票”)。在('click','#up',函数()上{
animateDiv(“背景绿”,真);
});
$(“#投票”)。在('click','#down',函数()上{
animateDiv(“背景红色”,假);
});
$(“#投票”)。在('click','#unnote',函数()上{
$('#Vote').html($defaultDiv);
});
函数animateDiv(bgColor,向上投票){
$('#Vote').html($animatedDiv.replace(“{bg}}”),bgColor)
.替换(“{vote}”,向上投票“,”向上投票“:”向下投票“);
$(“#aniDiv”)。制作动画({
宽度:“10px”
});
美元(“#投票”)。宽度(“350px”);
活动授权是一条路要走 我可能会在这里吹毛求疵,但这也让编写代码变得有趣,而不是陷入一片混乱。有一些建议可以让你的生活更轻松 避免内联样式。改用类。(有助于分离关注点) 使用函数避免重复代码 HTML
<div id='Vote' style="margin:0">
<a class="p-small bg-green" id='up' href="#">Up</a>
<a class="p-small bg-red" id='down' href="#">Down</a>
</div>
JAVASCRIPT
var $defaultDiv = "<a class='p-small bg-green' id='up' href='#'>Up</a>"
+ "<a class='p-small bg-red' id='down' href='#'>Down</a>",
$animatedDiv = "<div id='aniDiv' class='base {{bg}}'></div>"
+ "<div class='overlay'>"
+ "<a class='black' id='unvote' href='#'>You {{vote}}. Click to take vote back.</a></div>";
$('#Vote').on('click', '#up', function() {
animateDiv("bg-green", true);
});
$('#Vote').on('click', '#down', function() {
animateDiv("bg-red", false);
});
$('#Vote').on('click', '#unvote', function() {
$('#Vote').html($defaultDiv);
});
function animateDiv(bgColor, upVoted) {
$('#Vote').html($animatedDiv.replace("{{bg}}", bgColor)
.replace("{{vote}}", upVoted ? "upvoted" : "downvoted"));
$("#aniDiv").animate({
width: "10px"
});
$("#Vote").width("350px");
var$defaultDiv=“”
+ "",
$animatedDiv=“”
+ ""
+ "";
$(“#投票”)。在('click','#up',函数()上{
animateDiv(“背景绿”,真);
});
$(“#投票”)。在('click','#down',函数()上{
animateDiv(“背景红色”,假);
});
$(“#投票”)。在('click','#unnote',函数()上{
$('#Vote').html($defaultDiv);
});
函数animateDiv(bgColor,向上投票){
$('#Vote').html($animatedDiv.replace(“{bg}}”),bgColor)
.替换(“{vote}”,向上投票“,”向上投票“:”向下投票“);
$(“#aniDiv”)。制作动画({
宽度:“10px”
});
美元(“#投票”)。宽度(“350px”);
活动授权是一条路要走 我可能会在这里吹毛求疵,但这也让编写代码变得有趣,而不是陷入一片混乱。有一些建议可以让你的生活更轻松 避免内联样式。改用类。(有助于分离关注点) 使用函数避免重复代码 HTML
<div id='Vote' style="margin:0">
<a class="p-small bg-green" id='up' href="#">Up</a>
<a class="p-small bg-red" id='down' href="#">Down</a>
</div>
JAVASCRIPT
var $defaultDiv = "<a class='p-small bg-green' id='up' href='#'>Up</a>"
+ "<a class='p-small bg-red' id='down' href='#'>Down</a>",
$animatedDiv = "<div id='aniDiv' class='base {{bg}}'></div>"
+ "<div class='overlay'>"
+ "<a class='black' id='unvote' href='#'>You {{vote}}. Click to take vote back.</a></div>";
$('#Vote').on('click', '#up', function() {
animateDiv("bg-green", true);
});
$('#Vote').on('click', '#down', function() {
animateDiv("bg-red", false);
});
$('#Vote').on('click', '#unvote', function() {
$('#Vote').html($defaultDiv);
});
function animateDiv(bgColor, upVoted) {
$('#Vote').html($animatedDiv.replace("{{bg}}", bgColor)
.replace("{{vote}}", upVoted ? "upvoted" : "downvoted"));
$("#aniDiv").animate({
width: "10px"
});
$("#Vote").width("350px");
var$defaultDiv=“”
+ "",
$animatedDiv=“”
+ ""
+ "";
$(“#投票”)。在('click','#up',函数()上{
动画