简体   繁体   English

你如何在 jQuery 中设置一个永久值

[英]How do you set a permanent value in jQuery

I want to set a permanent value to a div element with jQuery;我想使用 jQuery 为 div 元素设置一个永久值;

the full js file:完整的js文件:

$(function () {
    var $a;
    var $b;
    $('button').click(function () {
        $a = $('#a').val();
        $b = $('#b').val();

        var $big = getBigger($b, $a);
        var $small = getSmaller($b, $a);
        $('#bigger').text($big);
        $('#smaller').text($small);
    });


 });


//a simple function for getting a bigger element
function getBigger(a, b) {
    return a ^ (a ^ b) & -(a < b);
}

//a simple function for getting a smaller element
function getSmaller(a, b) {
    return (a < b) ? a : b;
}

the full html file:完整的 html 文件:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="style/style.css">
    <title>Compare</title>
</head>
<body>
    <form>
        Enter a: <input type="number" id="a"><br/>
        Enter b: <input type="number" id="b"><br/> 
        <button> Compare</button> 
        <div id="bigger"></div> 
        <div id="smaller"> </div>
    </form>


    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="js/app.js"></script>
</body>
</html>

The problem is that when I leave the scope the variable gets destroyed问题是,当我离开范围时,变量会被破坏

PS.附注。 Now I realise that my mistake is that when a <button> is added in a form element, everytime the button is pressed, the data in the form gets reseted现在我意识到我的错误是在form元素中添加<button>时,每次按下按钮时,表单中的数据都会被重置

When you run code within a function it creates a scope.当您在函数内运行代码时,它会创建一个作用域。 Each variable you define using the var statement will be available only in that scope.您使用var语句定义的每个变量将仅在该范围内可用。

To create a variable for usage outside of the current scope, either declare a variable without putting var beforehand or directly write window.varname = value .要创建一个在当前作用域之外使用的变量,要么声明一个变量而不预先放置var要么直接写window.varname = value Have another read on that topic . 再读一读该主题

Besides that, in your code $('#bigger').text($big);除此之外,在你的代码中$('#bigger').text($big); the var $big never even gets defined, so maybe that is the problem. var $big甚至从未被定义过,所以也许这就是问题所在。

And setting an elements text works independent from scopes or variables.并且设置元素文本的工作独立于范围或变量。

Try this尝试这个

To Access:访问:

$("#a").data('value');

To Set设置

$("#a").data('value' , 'whatevervalue');

To Access Without JQuery:不使用 JQuery 访问:

document.getElementById("a").dataset.value;

To Set Without JQuery;不使用 JQuery 进行设置;

document.getElementById("a").dataset.value = "Some Value";

A Div cannot hold the Value attribute unless you use:除非您使用,否则 Div 不能保存 Value 属性:

document.getElementById("a").setAttribute("value" , "val");

Then to Assign it to Inside your div:然后将其分配给您的 div 内部:

$("#myDivElement").html($("#a").data('value'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM