通过id获取div值 - Javascript

时间:2011-04-13 03:11:28

标签: php javascript drupal html

我正在尝试将我的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>';

这是输出: enter image description here 请告诉我你是否需要更多,因为有:) 提前致谢

2 个答案:

答案 0 :(得分:1)

您是否在PHP代码中定义了$displaycur?此外,它应该是document.getElementById

更新:PHP所需要的就是汇率。您可以在JavaScript中执行其他所有操作:

<script type="text/javascript">
var exchangerate = <?php echo currency_api_convert("HKD", "CNY"); ?>;
var div = document.getElementById('record-price');
var price = parseInt(div.innerHTML);

// convert and display
alert(price * exchangerate);
</script>

答案 1 :(得分:1)

<?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";
?>

我猜'记录价格'是您的DIV的ID ...因此displaycur.innerHTML将包含<div></div>之间的所有内容。

<小时/> 修改

function showCurrency() {
  alert(document.getElementById('record-price').innerHTML);
  }