javascript 在 FBJS/Facebook 中使用 getElementById().getValue 的问题!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5286978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problem with using getElementById().getValue in FBJS/Facebook !
提问by Ahsan
I am using this code to generate hidden HTML code in Facebook :
我正在使用此代码在 Facebook 中生成隐藏的 HTML 代码:
echo "<div id=\"y257y\" style=\"display:none;\">".$fdesc."</div>";
And, I am trying to get this element back in JS using the following code
而且,我正在尝试使用以下代码在 JS 中获取此元素
newVal=document.getElementById('y257y').getValue();
But, I am getting an error: Undefined
但是,我收到一个错误:未定义
Can anyone kindly help me out ?
任何人都可以帮助我吗?
Thanks.
谢谢。
- ahsan
- 阿山
回答by rsp
Instead of:
代替:
newVal = document.getElementById('y257y').getValue();
try using:
尝试使用:
newVal = document.getElementById('y257y').innerHTML;
Are you using any JavaScript library, like jQuery or Prototype? If you're using jQuery:
您是否使用任何 JavaScript 库,例如 jQuery 或 Prototype?如果您使用 jQuery:
newVal = $('#y257y').html();
Other suggestions:
其他建议:
Use hidden form element:
使用隐藏的表单元素:
echo "<input type=hidden id=y257y value=\"$fdesc\">";
and in JavaScript:
在 JavaScript 中:
newVal = document.getElementById('y257y').value;
Or just output a <script>tag:
或者只是输出一个<script>标签:
echo "<script>newVal = \"$fdesc\";</script>";
and there's no need to find the value in the DOM – it's already in JavaScript.
并且不需要在 DOM 中查找值——它已经在 JavaScript 中了。
回答by Tom Gruner
Try using a textarea? Since at least that may have a getValue method if you are really lucky:
尝试使用文本区域?如果你真的很幸运,至少它可能有一个 getValue 方法:
$fdesc = str_replace('<', '<', $fdesc);
$fdesc = str_replace('>', '>', $fdesc);
echo "<textarea id=\"y257y\" style=\"display:none;\">".$fdesc."</textarea>";
newVal=document.getElementById('y257y').getValue();
newVal = newVal.replace(">", ">")
newVal = newVal.replace("<", "<")
But something tells me that is not going to work for you, facebook may not like it. Seems easier just to do this:
但是有些东西告诉我这对您不起作用,Facebook 可能不喜欢它。这样做似乎更容易:
$fdesc = <<<EOD
<div>Here is your html</div>
<p>And some more </p>
EOD;
$fdesc = str_replace("'", "\'", $fdesc); //Escape single quotes
$fdesc = str_replace("\n", "";', $fdesc); //Get rid of line breaks
echo "var fdesc = '$fdesk';";

