简体   繁体   English

使用javascript在onclick事件后更改iframe中元素的值

[英]Change value of element inside iframe after onclick event with javascript

I have some elements inside an iframe with an onclick event associated, something like: 我在iframe中具有与onclick事件相关联的一些元素,例如:

<iframe>
 ... 
    <input type="text" onclick="somefunction(this)"/>
 ...
</iframe>

In the function called I try to change the value of the input, nothing special: 在我尝试更改输入值的函数中,没有什么特别的:

{
...
foo.value = "changed!"
...
}

At this point I can see in firebug that the value has changed but when the function finish the value doesn't change at all. 此时,我可以在Firebug中看到该值已更改,但是当函数完成时,该值完全没有更改。

The same kind of thing with an onmouseover seems to work. 鼠标悬停时,同样的事情似乎起作用。

Any ideas? 有任何想法吗?

Thanks. 谢谢。

Edit: Change code mistake 编辑:更改代码错误

'this' means different things in the different contexts “这”在不同背景下意味着不同的事物

Given: 鉴于:

<input type="text" onclick="somefunction(this)"/>

'this' is the input (since it is called as myInput.onclick()) “ this”是输入(因为它被称为myInput.onclick())

But: 但:

function someFunction(foo) {
    this.value;
}

You are calling as: 您的呼叫方式是:

someFunction(myInput)

which is the same as 这与

window.someFunction(myInput)

So in that context, 'this' is the window object. 因此,在这种情况下,“ this”是窗口对象。

Use 'foo' instead (since that is the argument name in the function declaration). 请改用“ foo”(因为这是函数声明中的参数名称)。

Value is not defined for an iframe tag. 未为iframe广告代码定义值。 In Javascript, the "value" member is merely added to the object when set, but of course not taken into account by the navigator. 在Javascript中,“值”成员仅在设置时才添加到对象,但导航器当然不会考虑。

For setting content (this is also the case for div), you should use : 要设置内容(div也是如此),应使用:

this.innerHTML = "Changed!"; this.innerHTML =“已更改!”;

or 要么

this.innerText = "Changed!"; this.innerText =“已更改!”;

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

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