简体   繁体   English

使用javascript或..动态设置推文按钮'数据文本'内容?

[英]set Tweet button 'data-text' contents dynamically with javascript, or..?

Here is functioning code on a form that displays a Tweet button -- the button's on a form that displays several images -- when the user clicks one of the images, it becomes the 'selected' image and the Tweet button is supposed to tweet the selected image's name and url: 以下是显示推文按钮的表单上的功能代码 - 显示多个图像的表单上的按钮 - 当用户单击其中一个图像时,它变为“选定”图像,推文按钮应该发布推文所选图片的名称和网址:

      <a id="tweetBtnId" href="https://twitter.com/share" class="twitter-share-button" 
         data-text="Check me out on OurWebSite!"
         data-url=http://$ourSiteURL
         data-via=http://$ourSiteURL data-size="medium" data-count="none">Tweet</a>
     <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0]; 
            if(!d.getElementById(id)){js=d.createElement(s); js.id=id; 
              js.src="//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js,fjs);}}
                (document,"script", "twitter-wjs");</script>   

I have an 'onclick()' handler for the div that displays the image. 我有一个显示图像的div的'onclick()'处理程序。 When the user clicks one of the images, its div's onclick() handler is called and sets that image to be the 'currentlySelectedImage' on the page -- and the onclick() handler then needs to update the Tweet button's 'data-text' attribute with the name of the just-selected image: 当用户单击其中一个图像时,会调用其div的onclick()处理程序并将该图像设置为页面上的“currentSelectedImage” - 然后onclick()处理程序需要更新Tweet按钮的“数据文本”属性与刚刚选择的图像的名称:

          // This is part of the code of the 'onclick()' handler for
          // the image being selected. 
         <script> 
         function handleImageOnClick()
         {
           var myDynamicTweetText = "name of currently-selected image goes here";
           var elem = document.getElementById("tweetBtnId");
           alert("The elem is: " + elem);  // elem IS NULL !!  Dagnabbit.

           // this fails because 'elem' is null
           elem.setAttribute("data-text", myDynamicTweetText);

           // other onclick() code not shown for brevity......
         }
         </script>

I need to dynamically change the 'data-text' attribute's value in the Tweet button to be the name of the selected image. 我需要动态更改Tweet按钮中的'data-text'属性值作为所选图像的名称。 I added the javascript code above which fails-- the 'elem' obtained from the code here: 我添加了上面的javascript代码失败 - 从这里的代码获得的'elem':

    var elem = document.getElementById("tweetBtnId");

is null (I think) because of this line in the Twitter tweet button code above: 是null(我认为)因为上面Twitter推文按钮代码中的这一行:

   if(!d.getElementById(id)){js=d.createElement(s); js.id=id;

I'm not sure but it looks like the Twitter Tweet button default script overwrites any attempt to add an 'id' attribute to the Tweet button. 我不确定,但看起来Twitter Tweet按钮默认脚本会覆盖任何向Tweet按钮添加'id'属性的尝试。

You will see that I added the id="tweetBtnId" to the Tweet button above so I could get access to the Tweet button in my image-selection onclick() handler above, then set the 'data-text' to the name of the just-selected image. 您将看到我在上面的Tweet按钮中添加了id =“tweetBtnId” ,因此我可以在上面的图像选择onclick()处理程序中访问Tweet按钮,然后将'data-text'设置为刚选择的图像。

I just doubt that Twitter's design goal for the Tweet button was "we're gonna dumb this sucker WAY down, we'll only let these animals choose ONE data-text value - every Tweet button has to have one hard-coded, "Once-on-the-page" data-text attribute - joke's on them if they try to dynamically change the Tweet button's data-text attribute." 我只是怀疑Twitter的Tweet按钮的设计目标是“我们会愚蠢地将这个傻瓜推倒,我们只会让这些动物选择一个数据文本值 - 每个Tweet按钮必须有一个硬编码,”一次在页面“数据 - 文本属性 - 如果他们试图动态更改Tweet按钮的数据文本属性,就开玩笑。”

I need to get this to work -- any ideas? 我需要让这个工作 - 任何想法?

Just put it in a container with a known ID and traverse the document with it: 只需将其放入具有已知ID的容器中并使用它遍历文档:

<div id="someIDiKnow">
    <a id="tweetBtnId"...>...</a>
</div>

$("#someIDiKnow a").attr("data-text", "Replacement Text!");

You could use JQuery to update the attribute data-text by adding an onclick action on the image. 您可以使用JQuery通过在图像上添加onclick操作来更新属性数据文本 If you wanted to include the text inside the actual image tag by adding your own attribute to the tag, that could be an easy work around to assembling the tweet text. 如果您希望通过将自己的属性添加到标记中来将文本包含在实际图像标记内,则可以轻松地组合推文文本。

For example: 例如:

function updateTweetBtn(obj) {
   $('#someIDiKnow a').attr('data-text', $(this).attr('tweet'));
}

<img src="/images/myimage.jpg" tweet="This is what I want to tweet" onclick="updateTweetBtn(this);" />
<img src="/images/myimage2.jpg" tweet="This is some other text I want to tweet" onclick="updateTweetBtn(this);" />

using in html 在html中使用

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<a href="http://twitter.com/share" class="twitter-share-button" data-text="old text">Tweet</a>

I managed to change the 'data-text' with javascript 我设法用javascript更改'data-text'

var a = "new text";
document.getElementsByClassName('twitter-share-button')[0].setAttribute("data-text", a);

also worked with document.getElementById('twitter') if you add id='twitter' to <a> 如果你将id ='twitter'添加到<a>,也可以使用document.getElementById('twitter')

Looks like it's going to be the kludgy hidden form/php variable approach for now -- I'll post back if I find a better work-around. 看起来它现在将是kludgy隐藏形式/ php变量方法 - 如果我找到更好的解决方法,我会回发。

EDIT: the hidden-form/PHP variable solution has beaten the roadblock put up by Twitter's Tweet button -- I can now successfully, dynamically change the Tweet button text at will, anytime based on the user's client-side input. 编辑:隐藏形式/ PHP变量解决方案击败了Twitter的推文按钮设置的障碍 - 我现在可以成功地,根据用户的客户端输入随时动态更改推文按钮文本。 Per Domenic's astute observation that the question above is too long and has code in it, I'll skip posting the answer here, and I apologize for the length above. Per Domenic精明地观察到上面的问题太长并且有代码,我会跳过这里的答案,我为上面的长度道歉。

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

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