我正在尝试将我的 div 值获取到 Javascript 中。这是一个 Drupal 网站,我想转换货币并在 javascript 弹出窗口中显示它们。
到目前为止我有这个:
<?php
print "<script type='text/javascript'>";
$rate=currency_api_convert("HKD", "CNY");
print "exchangerate = ".$rate["value"].";";
print "var $displaycur = getElementById('record-price');";
print "$displaycur;";
print "</script>";
?>
我在浏览器中得到的是:
<script type="text/javascript">
exchangerate = 0.8406;var = getElementById('record-price');;
</script>
虽然我知道为什么会这样显示,但我仍然不知道如何在适当的变量中获取div 的ID。 有任何想法吗?我应该使用 innerHTML 吗?
更新: 部分源文件: page.tpl.php
<?php
// $Id: page.tpl.php,v 1.18.2.1 2009/04/30 00:13:31 goba Exp $
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language ?>" lang="<?php print $language->language ?>" dir="<?php print $language->dir ?>">
<head>
<?php print $head ?>
<title><?php print $head_title ?></title>
<?php print $styles ?>
<?php print $scripts ?>
<?php
// lets get the exchange rate from HKD to RMB, with the help of yahoo finance api and the currency api module
// we pass the value to javascript, the tooltip will handle the rest
print "<script type='text/javascript'>";
$rate=currency_api_convert("HKD", "CNY");
print "exchangerate = ".$rate["value"].";";
print "</script>";
?>
<script type="text/javascript">
function showCurrency() {
alert('$rate');
}
</script>
<!--[if lte IE 7]>
<?php print phptemplate_get_ie_styles(); ?>
<![endif]-->
<!--[if IE 6]>
<style type="text/css">
.fg-menu-ipod .fg-menu li { width: 95%; }
.fg-menu-ipod .ui-widget-content { border:0; }
.views-field-field-performer-value { margin-left:-150px;}
.views-field-field-instruments-value { margin-left:-150px;}
.coda-nav{display:block; position:absolute; width:400px;height:20px;top:260px;right:100px;z-index:125421;} </style>
<![endif]-->
</head>
<body<?php print phptemplate_body_class($left, $right); ?>>
还有node-record.tpl.php:
print '<div id="part1-right-line3">';
print '<div id="record-price">';
print uc_currency_format($node->sell_price);
print '</div>';
print '</div>';
print '<div id="part1-right-line4">';
print '<div id="add-to-cart">';
print drupal_get_form('uc_product_add_to_cart_form_'. $node->nid, $node);
print '</div>';
print '</div>';
print '</div>';
这是输出: 如果您需要更多,请告诉我,因为有 :) 提前致谢
最佳答案
<?php
print "<script type='text/javascript'>\n";
$rate=currency_api_convert("HKD", "CNY");
print "exchangerate = ".$rate["value"].";\n";
print "var displaycur = document.getElementById('record-price');\n";
print "alert(displaycur.innerHTML);\n";
print "</script>\n";
?>
我想 'record-price' 是您的 DIV 的 ID...所以 displaycur.innerHTML
将拥有 <div>
之间的所有内容和 </div>
.
编辑:
function showCurrency() {
alert(document.getElementById('record-price').innerHTML);
}
关于php - 通过 id 获取 div 值 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5643973/