开发者

Jquery .each method

learning Jquery and integrating with PHP - getting there, but have one last challenge in some code I'm working on.

I have HTML in a string, trying to pull html in tags, might be multiple elements in the HTML string, so trying to use each. My function worked fine without each, below is my each integration (returns nothing currently):

<?php 
   $info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li><li><strong><a href="http://www.mysite.com/test2.html" title="Some stuff">I want this text too</a></strong></li>';
   $info = json_encode($info);
?>

<script type="text/javascript">
$开发者_如何学JAVA(document).ready(function () {

    $("a", $( < ? php echo $info; ? > )).each(

    function () {
        alert($(this).html());
    });
};

This code below does work, but only returns the first element in the HTML:

<?php
   $info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li>';
   $info = json_encode($info);
?>

<script type="text/javascript">
$(document).ready(function () {
    var output = $("a", $( < ? php echo $info; ? > )).html();
    var link = $("a", $( < ? php echo $info; ? > )).attr("href");
    alert(output);
    alert(link);
});
</script>


This is a description and a working example of How to use .each() LINK
You can try this one as a example

$("a").each(function(index){alert($(this).html()});


Your code is not working because there are a few syntax issues.

First, change

< ? php echo $info; ? >

to

<?php echo $info; ?>

PHP doesn't like spaces and the opening and closing tags must appear without spaces.

Second, close the ready function and the script tag properly. Instead of

};

use,

});
</script>

Why are you encoding a piece of XML with JSON? That makes like no sense at all. Both are ways to encode data. HTML is XML too, btw. You can directly reference the $info variable since PHP will process everything first on the server.

<?php 
   $info = '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li>';
?>
<script type="text/javascript">
$(document).ready(function() {

    $("a", $("<?php echo $info; ?>")).each(
        function () {
            alert($(this).html());
        }
    );
});

Or just remove the temporary variable altogether. Makes it combersome to read, but that's essentially what PHP is doing.

$("a", $("<?php echo '<li><strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong></li>'; ?>")).each(

Or to make it even simpler, since you already have the HTML, simply include it as part of the page and maybe give it an ID to make referencing it easier with jQuery.

<li id="myList">
    <strong><a href="http://www.mysite.com/test.html" title="Some stuff">I want this text</a></strong>
</li>

<script type="text/javascript">
$(document).ready(function() {
    $("a", "#myList").each(function() {
        alert($(this).html());
    });
});
</script>

This last example gets rid of PHP completely, but you weren't really using it anyways.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜