【问题标题】:Get div value by id - Javascript通过 id 获取 div 值 - Javascript
【发布时间】:2011-04-13 03:11:28
【问题描述】:

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

这是输出: 如果您需要更多,请告诉我,因为有:) 在此先感谢

【问题讨论】:

  • 我对此感到非常困惑:var = getElementById('record-price');;
  • 感谢cmets,我想将div值放在一个变量中,这样我就可以用它来转换货币了

标签: php javascript drupal html


【解决方案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";
?>

我猜 'record-price' 是您的 DIV 的 ID...所以 displaycur.innerHTML 将包含 &lt;div&gt;&lt;/div&gt; 之间的所有内容。


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

【讨论】:

  • 是的,'record-price' 确实是 DIV 的 ID。当我用你的代码替换我的代码时,我收到了 missing variable name 错误:
  • @Michiel - 我可以在线查看完整的输出吗?
  • @Michiel - 也许您可以在原始问题中发布源代码?我对此很好奇。
  • Sweet,你可以找到它here,我会在我原来的问题中添加代码!
  • @Michiel - 现在它可以正确读取 DIV 的值。请删除该 F *** 评论 :)
【解决方案2】:

您是否在 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>

【讨论】:

  • 谢谢!不,我之前没有在我的 PHP 代码中定义它。我应该在javascript里面做吗?
  • @Michiel:PHP 认为$displaycur 是一个变量,并在输出中替换它的值。如果这不是故意的,您应该删除或转义 $ 符号。
  • 我只想将 div 的值放在一个变量中,以便进行转换。但是既然是 Drupal (php),我就必须这样,不是吗?
  • @casablance,感谢您的回答,但我不断收到错误消息 (div is null-error)。在我的代码中,我得到var exchangerate = Array
  • @Michiel,好吧,那么你的 DIV 有问题; 'div is null' 错误!?你能在输出源中看到那个 DIV 吗?在我的示例中,您有 'missing variable name' 错误,所以我猜 DIV 是一个问题。
猜你喜欢
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多