0

我想在 ajax 的帮助下显示所选图像的幻灯片,并且获取标题对于显示相应的幻灯片非常重要,但是在JavaScript 中,单击图像的标题没有获取。

javascript:

function slide(s) {
    var _event = s;
    alert(_event);
}

代码:

<div id="inner_body">
    <?php
    $c = mysql_connect("localhost", "abc", "xyz");
    mysql_select_db("root");
    $sql = "select * from images where year=2000";
    $qc = mysql_query($sql);
    $count = 0;
    while ($ans = mysql_fetch_array($qc)) {
        $title = ucwords($ans['event']);
        print " 
            <div class='img-wrap' onclick='slide($title)'>
                <img id='display_img' src='images/thumbnails/$ans[image1]'>
                <div class='img-overlay'>
                    <b1>" . $title . "</b1>
                </div>
            </div>";
    }
    ?>
</div>
4

2 回答 2

1

缺少引号标签

onclick='slide($title)'; //render onclick='slide(xxxx)'
//should be
onclick='slide(\"$title\")'; //render onclick='slide("xxxx")'

PS。

图片标签必须关闭<image /><image></image>

<b1>未定义(也是<b></b>过时的旧版 HTML)。这应该是<strong>...</strong>

于 2013-08-10T12:59:22.363 回答
0

传递thisonclick 并使用带有类的 jquery 查找子元素title获取 html 的值。

Javascript

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
    function  slide(s)
    {
        alert($(s).find('.title').html());   
    }
</script>

代码

    <div id="inner_body">
    <?php
    $c = mysql_connect("localhost", "abc", "xyz");
    mysql_select_db("root");
    $sql = "select * from images where year=2000";
    $qc = mysql_query($sql);
    $count = 0;
    while ($ans = mysql_fetch_array($qc)) {
        $title = ucwords($ans['event']);
        print " 
            <div class='img-wrap' onclick='slide(this)'>
            <img id='display_img' src='images/thumbnails/$ans[image1]'>
            <div class='img-overlay'>
             <b1 class='title'>" . $title . "</b1>
             </div>
            </div>";
    }
    ?>
</div>
于 2013-08-10T13:08:52.207 回答