简体   繁体   English

输出文本没有以正确的格式显示

[英]Output Text Doesn't Show in Correct Format

Here is the JavaScript I use to animate slider (fade effect) of the content I read from database: 这是我用来动画化我从数据库中读取的内容的滑块(淡入效果)的JavaScript:

<script language="javascript">
jQuery(document).ready(function ()
{
    var terms = ["span_1","span_2"];
    var i = 0;

    function rotateTerm() {
        jQuery("#text-content").fadeOut(200, function() {
          jQuery(this).text(jQuery('#text-slider .'+terms[i]).html()+i).fadeIn(200);
        });
        jQuery("#title-content").fadeOut(200, function() {
          jQuery(this).text(jQuery('#title-slider .'+terms[i]).html()+i).fadeIn(200);
           i == terms.length - 1 ? i=0 : i++;
        });
    }
    rotateTerm();
    setInterval(rotateTerm, 1000);   
});
</script>

And here is the PHP code I use: 这是我使用的PHP代码:

<?php
    if (!empty($testLst)) :     
        $num=1; 
        foreach($testLst as $key=>$item):
             $item->slug = $item->id;
             $item->catslug = $item->catid ;

?><div id="hidden-content" style="display:none;">       
    <div id="title-slider"> 
        <span class="<?php echo 'span_'.$num; ?>">
            <h4><a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($item->id, $item->catid)); ?>">
                <?php echo $item->title; ?></a>
            </h4>
        </span>
    </div>
<div id="text-slider">
    <span class="<?php echo 'span_'.$num; ?>">
        <p>
            <?php
                $concat=array_slice(explode(' ',$item->introtext),0,20);
                $concat=implode(' ',$concat);
                echo $concat."...";
            ?>
        </p>
    </span>
</div></div>
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($item->id, $item->catid)); ?>" title="Learn more" class="learnMore">Learn more &gt;&gt;</a></p>
<?php
    $num++;
    endforeach;
    endif;
?>

<div id="title-content">
</div>
<div id="text-content">
</div>

And here is a JSFiddle page reproducing what I would like to do. 这是一个JSFiddle页面,再现了我想做的事情。

My problem is that I am getting data that still has HTML tags, however I would like the output to have my CSS styles. 我的问题是我得到的数据仍然带有HTML标记,但是我希望输出具有CSS样式。

You could clone the node, and set that to be the new content of the target elements, to keep everything in jQuery objects, but personally, I'd use the .outerHTML property. 您可以克隆节点,并将其设置为目标元素的新内容,以将所有内容保留在jQuery对象中,但就我个人而言,我将使用.outerHTML属性。

I've updated your fiddle to show you what I mean: I've changed the .text(...set content here) to .html() , because we're injecting HTML content. 更新了您的小提琴以显示您的意思:我将.text(...set content here)更改为.html() ,因为我们正在注入HTML内容。 Then, I added [0] at the end of your selector, to return the raw element reference, which gives access to all standard JS properties and methods an element has, and just went ahead and fetched the outerHTML ... easy-peasy 然后,我在选择器的末尾添加了[0] ,以返回原始元素引用,该引用提供对元素具有的所有标准JS属性和方法的访问权,然后继续并获取了outerHTML ... easy-peasy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM