如何将嵌套的全局或本地jquery变量调用到html中

how can I call a nested global or local jquery variable into html?

本文关键字:变量 jquery 调用 html 嵌套 全局      更新时间:2023-09-26

我遇到了这个问题,我已经试了好几天了,但没有成功。如有任何帮助,我们将不胜感激。

我想将嵌套jQuery中的一个变量调用到html中。我称之为"问题"的变量。我甚至试图将其全局化,但当我在功能中本地更新时

$(".dropdown dd ul li a").click(function() { }

这些变化仍然是局部的。我该怎么做?非常感谢。注意:我已经通过粘贴整个功能进行了更新。我在网上找到了这个功能,并对其进行了修改,使其同时执行图像和文本的选择功能。我想知道当这种情况发生时,它是像facebook这样的选择,这样我就可以用facebook页面更改注册页面,

 <span =email>

上面的部分帮助我知道它的电子邮件或其他社交媒体网站。关于运行警报(文本)

显示

<span =email=""><img class="flag flagvisibility" src="media/Email_icon.png" alt="" style="margin-bottom:0px" height="30" width="30" align="left&quot;"> Email</span>

我用

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)

以获得等号"="的第一个和第二个标记,如果它的电子邮件或其他社交媒体网站,我可以从中提取。我希望这能帮助

这是整个

</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;event.preventDefault();来阻止链接返回。

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.