【问题标题】:Adding a new element HERE where the code is, not after another element在代码所在的地方添加一个新元素,而不是在另一个元素之后
【发布时间】:2011-06-13 16:08:49
【问题描述】:

为 phpBB 制作了一些 BBcode 以允许用户发布带有特定标签的 flickr 图片。 线程中的每个照片 div 都需要是唯一的,并且在线程加载时加载图像。

当涉及到唯一 DIV 时,我被困在一种方法中,即在插入 BBcode 时将唯一命名的元素插入到 DOM 中,然后加载图片。看来我不能在 BBcode 中使用 PHP,也不能使用模板标签 - 如果我可以轻松地从帖子 ID 和 flickr 标签中制作一个独特的照片元素,我会笑的。哦,我不能触摸模板。这一切都必须在 BBcode 中。 所以,下面是我制作唯一 ID 的方法:

 var flickrUser = "{URL}".split("/")[4];   
 var tag = "{URL}".split("/")[6];
 var photoDIV = flickrUser + "-" + "tag";

或者...在我可能使用的上方有一个使用唯一帖子 ID 调用的元素:

<div id="p61789" class="post bg2">

我试过了

var postnumber=$(this).closest('div[class^="post"]').attr('id');

但它似乎总是返回页面上第一个匹配的 div,而不是最接近 BBcode 的点。这个元素是下面的两个“div”

<div class = "content">

在用户发帖区下方有:

<div id="sig61789" class="signature">

所以我完全卡住的地方是导航到 prev() 或最近的() 或 parent() 或实际上从我所在的点开始的任何地方,而没有 $(this) 链接可供参考。

所以不应该是这样的:

$(this).prev('content').html('<ul class="thumbs" id=photoDIV></ul>');

甚至

$(this).html('<ul class="thumbs" id=photoDIV></ul>');

工作?每次我认为我理解 jquery 时,一切都会再次变得朦胧......

编辑:为 Pointy 添加了更多细节:

<div id="p63167" class="post bg2 online">
  <div class="inner">
    <span class="corners-top">
      <span></span>
    </span>
    <div class="postbody">
      <ul class="profile-icons">
        <li class="edit-icon">
          <a href="posting.php" title="Edit post">
            <span>Edit post</span>
          </a>
        </li>
        <li class="delete-icon">
          <a href="posting.php" title="Delete post">
            <span>Delete post</span>
          </a>
        </li>
        <li class="report-icon">
          <a href="report.php" title="Report this post">
            <span>Report this post</span>
          </a>
        </li>
        <li class="info-icon">
          <a href="mcp.php" title="Information">
            <span>Information</span>
          </a>
        </li>
        <li class="quote-icon">
          <a href="posting.php" title="Reply with quote">
            <span>Reply with quote</span>
          </a>
        </li>
      </ul>
      <h3 class="first">
        <a href="#p63167">Re: Testing new bbcode - ignore</a>
      </h3>
      <p class="author">
      <a href="viewtopic.php">
        <img src="" alt="Post" title="Post" />
      </a>by 
      <strong>
        <a href="memberlist.php">xxx</a>
      </strong>» 13 Jun 2011 14:33</p>
      <div class="content">
        <script>var
        APIkey="xxx";head.js("/forum/jflickrfeed/jflickrfeed.min.js","http://jquery-lazy.googlecode.com/svn-history/r14/trunk/jquery.lazy.source.js",function(){var
        flickrUser="http://www.flickr.com/photos/xxx/tags/7thjanuary2010/".split("/")[4];var
        tag="http://www.flickr.com/photos/xxx/tags/7thjanuary2010/".split("/")[6];var
        photoDIV=flickrUser+"-"+"tag";$(this).html('
        <ul class="thumbs" id="photoDIV">
        </ul>');$.getJSON("http://www.flickr.com/services/rest/?jsoncallback=?",{method:"flickr.urls.lookupUser",url:"http://www.flickr.com/photos/xxx/tags/7thjanuary2010/",format:"json",api_key:APIkey},function(data){$('#cbox').jflickrfeed({limit:30,qstrings:{id:data.user.id,tags:tag},itemTemplate:'
        <li>'+'
        <a rel="colorbox" href="{{image}}" title="{{title}}">'+'
        <img src="{{image_m}}" alt="{{title}}" />'+'</a>'+'</li>'},function(data){$('#cbox
        a').colorbox();});});$.lazy([{src:'/forum/jflickrfeed/colorbox/colorbox/jquery.colorbox-min.js',name:'colorbox',dependencies:{css:['/forum/jflickrfeed/jflickrfeed.css','/forum/jflickrfeed/colorbox/example1/colorbox.css']}}]);});</script>
        <ul id="cbox" class="thumbs"></ul>
      </div>
      <div id="sig63167" class="signature"></div>
    </div>
  </div>
</div>

【问题讨论】:

  • 如果您要张贴代表剪断页面的整个相关片段的样子,可能有人可以提供帮助。从您上面写的内容很难准确判断页面结构是什么样的。
  • 另外,当您尝试按类别选择某些内容时,您可以使用“div.post”;您不必使用属性选择器。
  • 谢谢,@pointy。我添加了更多细节。
  • 不不 - 你的 &lt;div&gt; 元素与你试图用“[class^=post]”选择器找到的类“post”就是我所说的。您只需使用“.post”即可做到这一点。
  • 问题是,就像我说的,代码对文档中的“点”一无所知;它在 DOM 中没有“位置”的概念。

标签: javascript jquery phpbb bbcode


【解决方案1】:

问题是您假设在脚本块中执行时this 上下文是脚本块本身,这是不正确的。如果没有明确给出上下文,那么你的上下文就是window DOM 元素。是否有任何原因您不能将此代码移动到页面级别并在页面加载时初始化所有帖子?

$(function() {
    $('.post').each(function(i,item) {
        console.log(this); //Post DOM element. 
        //execute your flick retrieval code here.
    });
}

【讨论】:

  • 啊啊啊啊啊!过去 12 小时实现浪费的灯泡,感谢您指出这一点。所以这并不意味着“这个”,我也了解到“生活”并不意味着“生活”。我想知道是否有人整理了一个 jquery 函数列表,这些函数并不意味着他们所说的?!我会试试你的功能——虽然当你说“在页面加载时初始化所有帖子”时,大约 20 个帖子中只有一篇有 flickr 图片;我会睡在上面,然后明天继续努力。谢谢!
  • @digitaltoast:add(我会加号)或aftera.after(b) 实际上将 b 放在 a 之后)或 closest 这是最接近的 parent 而不是最近的元素?实际上,它们对我来说似乎都很自然,但如果你按字面意思理解这些名称,它们可能意味着完全不同的东西——我猜短名称的危险。
  • @pointy - 感谢您的建议。在您的帮助和一些令人头疼的过程中,我知道有一种方法可以让用户在我们的论坛中发布图片。代码:pastebin.com/ZTyBreaR phpbb 的 bbcode:phpbb.com/community/viewtopic.php?p=13009040#p13009040 演示:reading-forum.co.uk/forum/…。非常感谢所有相关人员。
【解决方案2】:

您是否尝试过 Javascript 的 DOM 函数来添加它? http://www.javascriptkit.com/javatutors/dom2.shtml

例如,在当前脚本之后插入,id 为“script1”:

document.getElementById("script1").parentNode.appendChild( newelement );

您也可以添加原始 html,只需设置新元素的 innerHTML。

【讨论】:

  • 但是,如果我能够将唯一标识 ID 应用到脚本本身,那么生成 ul 元素的 ID 就不会有问题。除非我理解错了,否则这不是让我处于先有鸡还是先有蛋的境地吗?
  • 你能不能用
猜你喜欢
  • 2011-01-15
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 2011-06-05
  • 1970-01-01
相关资源
最近更新 更多