简体   繁体   English

使用javascript和jQuery更改变量名称

[英]Changing variable name with javascript and jQuery

How to shift between variable names in jQuery and change them? 如何在jQuery中的变量名之间切换并更改它们?

//predefined variables
var s1='';
var d2='';
//trying to change variables by .attr() parameter but no luck
$('body').on('click','span', function() {
var $(this).parent().attr('data-scan')=$(this).attr('id');
});

HTML HTML

<div data-scan="s1"><span id="banana">Banana</span><span id="apple">Apple</span></div>
<div data-scan="d2"><span id="orange">Orange</span><span id="apple">Apple</span></div>

How can I change specific variables? 如何更改特定变量? I do't care about changing attr papameter, all I need is changing predefined global var parameters! 我不关心改变attr papameter,我只需要更改预定义的全局var参数!

You are using wrong syntax of attr() 你使用错误的attr()语法

Syntax: attr( attributeName , value) 语法:attr(attributeName,value)

Change 更改

var $(this).parent().attr('data-scan')=$(this).attr('id');

To

$(this).closest('div').attr('data-scan',$(this).attr('id'));

You code would be 你的代码就是

Live Demo 现场演示

$('body').on('click','span', function() {
    debugger
 $(this).closest('div').attr('data-scan',$(this).attr('id'));

    s1=$(this).closest('div').attr('data-scan');
    alert(s1);
});​

Firstly, your html for the data-scan attributes is wrong, you have no closing quotes. 首先,您的data-scan属性的html是错误的,您没有收尾报价。

Secondly, you can the data() jquery function to access data attributes. 其次,您可以通过data() jquery函数来访问data属性。

Thirdly, you cannot set values by using the = operator. 第三,您不能使用=运算符设置值。

You want something like this: 你想要这样的东西:

 $(this).parent().data('scan', $(this).attr('id'));

or, without the data() function: 或者,没有data()函数:

 $(this).parent().attr('data-scan', $(this).attr('id'));

Here is a working example 这是一个有效的例子


To get the value you can do one of the following: 要获得该值,您可以执行以下操作之一:

var dataScan = $(this).parent().data('scan');

or 要么

var dataScan = $(this).parent().attr('data-scan');

Your exact requirements for setting a variable based on the data-scan value 您根据数据扫描值设置变量的确切要求

Based on your comments and code, I think it has not been clear what you were trying to do. 根据您的评论和代码,我认为您还不清楚自己要做什么。 I think I have worked it out though and you want to use the data-scan value to determine which global variable should be set... 我想我已经解决了,你想使用data-scan值来确定应该设置哪个全局变量...

//predefined variables
var s1='';
var d2='';

$('body').on('click','span', function() {
    var variableType = $(this).parent().data('scan');
    var valueToSet = $(this).attr('id');
    if(variableType == "s1"){
       s1 = valueToSet;
    }
    else if(variableType == "d2"){
       d2 = valueToSet;
    }
});

Here is an example of what I think you are trying to do. 这是我认为你想要做的一个例子

However, if you have lots of variables then it is not ideal to use so many if/else statements. 但是,如果你有很多变量,那么使用这么多if / else语句并不理想。 So you could use the javascript eval() function. 所以你可以使用javascript eval()函数。

var variableType = $(this).parent().data('scan');
var valueToSet = $(this).attr('id');

eval("" + variableType + " = '" + valueToSet + "';");

See here for an example 请看这里的例子

But be careful the your eval code is subjected to user injected values (not that javascript is safe from users anyway) 但是要小心,你的eval代码会受到用户注入的值(不管javascript对用户是否安全)

You're pretty close! 你很亲密!

// set
$('#item').attr('data-scan', 'set new value');

// get
var dataScan = $('#item').attr('data-scan');
console.log(dataScan); //=> set new value

Probably best if you use .prop() rather than .attr() 如果你使用.prop()而不是.attr()可能是最好的

$(this).parent().prop('data-scan', $(this).attr('id') );

As the jQuery api documentation states 正如jQuery api文档所述

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. 从jQuery 1.6开始,.attr()方法为尚未设置的属性返回undefined。 In addition, .attr() should not be used on plain objects, arrays, the window, or the document. 此外,.attr()不应用于普通对象,数组,窗口或文档。 To retrieve and change DOM properties, use the .prop() method. 要检索和更改DOM属性,请使用.prop()方法。

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

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