Javascript 如何将嵌套的全局或局部jquery变量调用到html中?
我有这个问题,我试了好几天,但没有成功。任何帮助都将不胜感激 我想将嵌套jQuery中的变量调用到html中。我用名称问题调用的变量。我甚至试图使它成为全局的,但当我在函数内部局部更新它时Javascript 如何将嵌套的全局或局部jquery变量调用到html中?,javascript,jquery,html,Javascript,Jquery,Html,我有这个问题,我试了好几天,但没有成功。任何帮助都将不胜感激 我想将嵌套jQuery中的变量调用到html中。我用名称问题调用的变量。我甚至试图使它成为全局的,但当我在函数内部局部更新它时 $(".dropdown dd ul li a").click(function() { } 这些变化仍然是局部性的。我该怎么做?非常感谢。 NB/I已通过粘贴整个函数进行更新。我在网上找到了这个函数,并对它进行了修改,使它能够同时使用图像和文本执行选择功能。我想知道当这种情况发生时,哪一个选择像faceb
$(".dropdown dd ul li a").click(function() { }
这些变化仍然是局部性的。我该怎么做?非常感谢。
NB/I已通过粘贴整个函数进行更新。我在网上找到了这个函数,并对它进行了修改,使它能够同时使用图像和文本执行选择功能。我想知道当这种情况发生时,哪一个选择像facebook,这样我就可以用facebook页面更改注册页面
<span =email>
要获得等号=的第一个和第二个标记,我可以从中提取它的电子邮件或其他社交媒体网站。我希望这有帮助
这是全部
</style>
<script type="text/javascript">
var PROBLEM =[]
$(document).ready(function() {
$(".dropdown img.flag").addClass("flagvisibility");
$(".dropdown dt a").click(function() {
$(".dropdown dd ul").toggle();
return false; //This is the important part
});
$(".dropdown dd ul li a").click(function() {
var text = $(this).html();
$(".dropdown dt a span").html(text);
$(".dropdown dd ul").hide();
//$("#result").html(text);
//$("#result").html("Selected value is: " + getSelectedValue("sample"));
$("#result").html("Selected value is: " + text);
var n1 = text.indexOf("=") + 1;
var n2 = text.indexOf("=", n1);// OR SIMPLY USE c=text.slice(text.indexOf("=")+1, text.indexOf("=", text.indexOf("=")+1))
n3 = text.slice(n1, n2)
PROBLEM[0] = text.slice(n1, n2)
//return false; //This is the important part
})
function getSelectedValue(id) {
return $("#" + id).find("dt a span.value").html();
}
$(document).bind('click', function(e) {
var $clicked = $(e.target);
if (! $clicked.parents().hasClass("dropdown"))
$(".dropdown dd ul").hide();
});
$("#flagSwitcher").click(function() {
$(".dropdown img.flag").toggleClass("flagvisibility");
});
});
</script>
<dl id="sample" class="dropdown">
<dt style="vertical-align:middle"><a href="#"><span ><img class="flag" src="media/Email_icon.png" alt="" width="30" height="30" align=left" style="margin-bottom:0px"/> Email</span></a></dt>
<dd>
<ul>
<!--the use of names like email, face book in the span is just for some dirty steps in the javascript above .indexOf("=")-->
<li><a href="#"><span =email><img class="flag" src="media/Email_icon.png" alt="" width="30" height="30" align=left" style="margin-bottom:0px" /> Email</span></a></li>
<li><a href="#"><span =facebook><img class="flag" src="media/Facebook_icon.png" alt="" width="30" height="30" align=left" style="margin-bottom:0px" /> Facebook</span></a></li>
<li><a href="#"><span =twitter><img class="flag" src="media/Twitter_icon.png" alt="" width="30" height="30" align=left" style="margin-bottom:0px" /> Twitter</span></a></li>
<li><a href="#"><span =linkedin><img class="flag" src="media/Linkedin_icon.png" alt="" width="30" height="30" align=left" style="margin-bottom:0px" /> LinkedIn</span></a></li>
</ul>
</dd>
</dl>
<span id="result">
我认为问题是因为您没有阻止超链接默认操作的发生。您需要使用return false;或事件违约;阻止链接发回
var PROBLEM = [];
$(document).ready(function() {
$(".dropdown img.flag").addClass("flagvisibility");
$(".dropdown dt a").click(function() {
$(".dropdown dd ul").toggle();
return false; //This is the important part
});
$(".dropdown dd ul li a").click(function() {
var text = $(this).html();
$(".dropdown dt a span").html(text);
$(".dropdown dd ul").hide();
$("#result").html("Selected value is: " + text);
var n1 = text.indexOf("=") + 1,
n2 = text.indexOf("=", n1);
n3 = text.slice(n1,n2);
PROBLEM = text.slice(n1,n2);
return false; //This is the important part
}
});
此外,jQuery就绪事件的格式也不正确:
$(document).ready(function() {
}); // your missing this closing bracket.
除了给它赋值外,您不使用它。您是如何测试的?在填充问题变量后,您希望如何处理它?它通常不作为全局变量工作的原因是,单击处理程序将在将来运行一段时间,因此问题变量中的值在单击处理之后才会出现。如果你想在其他代码中使用PROBLEM,那么从click处理程序中调用该代码,并将PROBLEM变量作为参数传递给另一个函数,这将加载变量不再存在的新页面。或者您可以在使用PROBLEM with PROBLEM=;)的值后重置它;。另外,我只想声明var问题=[];是一个数组,您在单击处理程序中赋值,而不是按。可能您有一个或三个输入错误?您介绍的某些代码行不以分号结尾。这可能会导致错误,阻止某些事情发生。感谢您的评论,我没有粘贴整个jquery函数,但其余部分可以。我只是不知道如何在全球范围内进行本地更新variable@KevinOluoch你想用你的代码实现什么?你想让用户从下拉列表中的社交网络列表中进行选择吗?非常感谢你的回复,事实上我已经做到了。最后一行已经在做我想做的事情了,它可以嵌入到html中。原始代码可以在这里找到链接中的代码通过名称和图像更改下拉列表,我希望无论何时发生这种情况,我都会发送一条消息,将登录表单更改为我应用程序中的电子邮件或facebook
var PROBLEM = [];
$(document).ready(function() {
$(".dropdown img.flag").addClass("flagvisibility");
$(".dropdown dt a").click(function() {
$(".dropdown dd ul").toggle();
return false; //This is the important part
});
$(".dropdown dd ul li a").click(function() {
var text = $(this).html();
$(".dropdown dt a span").html(text);
$(".dropdown dd ul").hide();
$("#result").html("Selected value is: " + text);
var n1 = text.indexOf("=") + 1,
n2 = text.indexOf("=", n1);
n3 = text.slice(n1,n2);
PROBLEM = text.slice(n1,n2);
return false; //This is the important part
}
});
$(document).ready(function() {
}); // your missing this closing bracket.