简体   繁体   English

创建一个JavaScript函数来更改先前声明的变量

[英]Creating a JavaScript function to change a previously declared variable

I am having trouble changing a variable on a page with lots of PHP and JavaScript. 我在使用很多PHP和JavaScript更改页面上的变量时遇到麻烦。

This part is working fine for me: 这部分对我来说很好:

<script language="javascript" type="text/javascript">
function changebg(my)
{
    document.getElementById("imglayer").style.backgroundImage ='url('+ my.src +')';
    document.getElementById("cropframe").style.backgroundImage ='url('+ my.src +')';
}
var test2 = new String("url('+ my.src +')");
</script>

<img src="../../uploads/595MCoDFyArFFe.jpg" width="50" height="50" onclick="changebg(this)" >
<img src="../../uploads/P6l6J8aqzli6gh.jpg" width="50" height="50" onclick="changebg(this)" >
<img src="../../uploads/stXWS8fL4L3nvs.jpg" width="50" height="50" onclick="changebg(this)" >

with the div IDs being called for later. 稍后会调用div ID。

My issue is I have another variable that I need to change dynamically: 我的问题是我需要动态更改另一个变量:

<script type="text/javascript">
    var test1 = new String( "<?php $src_name = '" );
    var test2 = new String( "956ENbXjzTlkBo.jpg" );
    var test3 = new String( "'; // modify this, original file?>");" );
    document.write( String( test1, test2, test3 );
</script>

I need var test2 to change with the other two which are being grabbed by the div ID. 我需要使用var test2来更改由div ID抓取的其他两个。 How do I change that string value? 如何更改该字符串值? Is this even possible? 这有可能吗?

Any help would be greatly appreciated! 任何帮助将不胜感激!

First of all, this is not Java (and even in Java you do not really need it). 首先,这不是Java(甚至在Java中您也不需要它)。 You should not use new String(…) ; 您不应该使用new String(…) ; simply use string literals: 只需使用字符串文字:

var test1 = "…";

etc. 等等

Second, strings are immutable; 第二,字符串是不可变的。 you cannot change a string value/object; 您不能更改字符串值/对象; you can only create and assign a new string. 您只能创建和分配一个新字符串。 However, you can also create a user-defined Object instance that has a method that returns a value according to other values (like variable values): 但是,您也可以创建一个用户定义的Object实例,该实例具有一个根据其他值(例如变量值)返回值的方法:

var test2 = {
  toString: function () {
    return "foo" + test1 + "bar";
  }
};

The document.write() DOM method implicitly converts its argument to string, which is done by calling either the hull object's toString() or valueOf() method, whichever is available first (see the ECMAScript Language Specification, 5.1 Edition, section 9.8 ). document.write() DOM方法将其参数隐式转换为字符串,这可以通过调用船体对象的toString()valueOf()方法(以先到者为准)来完成(请参阅ECMAScript语言规范,5.1版,第9.8节 )。 。 So 所以

document.write(test2);

is equivalent to 相当于

document.write(String(test2));

( section 15.5.1 ) or 第15.5.1节 )或

document.write(test2.toString());

It will write the return value of the toString() method of the object referred to by the value of test2 . 它将写入由test2值引用的对象的toString()方法的返回值。 (For dynamic change of the output, you need to use other DOM features, such as the standards-compliant textContent , nodeValue , or the proprietary innerHTML property.) (要动态更改输出,您需要使用其他DOM功能,例如符合标准的textContentnodeValue或专有的innerHTML属性。)

BTW, the String constructor only considers one argument (see above), so new String(x, y, z) is equivalent to new String(x) . 顺便说一句, String构造函数仅考虑一个参数(请参见上文),因此new String(x, y, z)等效于new String(x) Perhaps you were looking for concatenation: new String(x + y + z) . 也许您正在寻找连接: new String(x + y + z) But if at least one operand is of type String , x + y + z suffices; 但是,如果至少一个操作数的类型为String ,则x + y + z就足够了; if not, String(x) + y + z or "" + x + y + z works. 如果不是,则使用String(x) + y + z"" + x + y + z

Just use the right quotes: 只需使用正确的引号即可:

var test2 = new String("url(" + my.src + ")");

Or simply: 或者简单地:

var test2 = "url(" + my.src + ")";

However, you can't change a php variable by writing php code from Javascript. 但是,您无法通过从Javascript编写php代码来更改php变量。 When the Javascript runs, the php script has aready ended. 当Javascript运行时,php脚本已结束。

First off, you're going a very weird way about building a string. 首先,您将采用一种非常奇怪的方式来构建字符串。 I assume the point of this is to, at the end of all of this, end up with a string that looks like this: 我认为这样做的目的是,在所有这些操作的最后,加上一个类似于以下内容的字符串:

<?php $src_name = 'X'; // modify this, original file ?>

(This outputs PHP, which won't be interpreted by your web server, but I'll assume you know that and you're outputting it for other reasons.) (这将输出PHP,您的Web服务器将不会解释该PHP,但是我假设您知道这一点,并且出于其他原因而正在输出它。)

Where x is some piece of data that's available in some other DOM element (one of your img elements, perhaps, since the filenames are similar). 其中x是其他DOM元素(也许是img元素之一,因为文件名相似)中可用的一些数据。 If you then want to, after some future arbitrary event occurs, update this x value that has been printed to the page. 然后,如果您希望在以后发生任意事件后,更新已打印到页面上的x值。 You'll need to give yourself some tangible element to update - for example, a span element. 您需要给自己一些有形的元素来更新-例如,一个span元素。

Try document.write() ing this : 尝试document.write() 这样做

document.write("&lt;?php $src_name = '<span id=\"my-element\">X</span>'; // modify this, original file ?&gt;");

Note I've changed your < and > to their encoded equivalents. 请注意,我已将<和>更改为它们的编码等效项。

Then, you can update the contents of your span using JavaScript at a later date (see below). 然后,您可以更新的内容span使用JavaScript在稍后的日期(见下文)。 This isn't changing the variable you have declared, but rather it is changing the contents of the page. 这不是在更改您已声明的变量 ,而是在更改页面的内容。 Since you are just printing your string at the end of it all (or, trying to, since String() won't concatenate the given strings like you seem to think it will), it seems to me like this is what you're trying to accomplish. 由于您只是在所有内容的末尾打印字符串(或者尝试这样做,因为String()不会像您认为的那样将给定的字符串连接起来),所以在我看来,这就是您的意思试图完成。

document.getElementById("my-element").innerText = "NEW VALUE";

Again, this is not updating the variable , per se, just updating the text that you are printing to the page. 同样,这并不是更新变量本身,而是更新要打印到页面的文本。 This seems to be your end goal anyhow, correct me if I'm wrong. 无论如何,这似乎都是您的最终目标,如果我错了,请纠正我。

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

相关问题 Javascript-覆盖先前在另一个函数中声明的样式 - Javascript - Overriding styles previously declared in another function JavaScript-可以在变量声明期间定义先前声明的对象的属性吗? - JavaScript - Is it okay to define a property of a previously declared object during a variable declaration? Javascript 函数()在之前声明的 object 中:“被调用”事件? - Javascript function() inside an object previously declared: 'was called' event? 可以在JavaScript中将函数声明为变量吗? - Can a function be declared as a variable in JavaScript? 覆盖先前声明的TypeScript函数 - Overwrite previously declared TypeScript function 为什么要为JavaScript中的变量分配声明的函数? - Why assign a declared function to a variable in javascript? 声明的javascript变量在JS函数中不起作用 - Declared javascript variable not working in a JS function 如何将已声明的javascript函数指向变量 - How to point already declared javascript function to a variable Javascript:如何将声明的函数存储在变量中? - Javascript: How to store a declared function in a variable? 如何让此 JavaScript 代码段发送一封电子邮件,其中正文是先前声明的变量? - How do I make this JavaScript snippet send an email with the body being a previously declared variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM