php-jqueryを介して別のdivから1つのdivに値を渡す
あるdivから別のdivに値を渡そうとしています。最初のdivはforeachループによって生成され、条件が一致した場合、値が2番目のdivに渡され、2番目のdivがjqueryポップアップウィンドウを介して表示されるという条件ステートメントがあります。 これが私のコードです:
<?php foreach ($value as $data) {
?>
<div class="center">
<h4>Description:</h4>
<?php
if(strlen($data['description'])<50)
{
echo $data['description'];
}
else
{
$str=$data['description'];
$substr=substr($str,0,50);
echo $substr.'<p class=button><a href=#>read more...<a/><p/>';
?>
<?php }?>
</div>
<?php }?>
<div class="popupContact">
<a class="popupContactClose">x</a>
<h1>Description</h1>
<p class="contactArea">
<?php echo $str;?>
</p>
</div>
<div class="backgroundPopup"></div>
これが私のjqueryポップアップです
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
//loading popup with jQuery magic!
function loadPopup(){
//loads popup only if it is disabled
if(popupStatus==0){
$(".backgroundPopup").css({
"opacity": "0.7"
});
$(".backgroundPopup").fadeIn("slow");
$(".popupContact").fadeIn("slow");
popupStatus = 1;
}
}
//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
$(".backgroundPopup").fadeOut("slow");
$(".popupContact").fadeOut("slow");
popupStatus = 0;
}
}
//centering popup
function centerPopup(){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $(".popupContact").height();
var popupWidth = $(".popupContact").width();
//centering
$(".popupContact").css({
"position": "absolute",
"top": windowHeight/2-popupHeight/3.25,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6
$(".backgroundPopup").css({
"height": windowHeight
});
}
//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
//LOADING POPUP
//Click the button event!
$(".button").click(function(){
//centering with css
centerPopup();
//load popup
loadPopup();
});
//CLOSING POPUP
//Click the x event!
$(".popupContactClose").click(function(){
disablePopup();
});
//Click out event!
$(".backgroundPopup").click(function(){
disablePopup();
});
//Press Escape event!
$(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup();
}
});
});
ここで、最初のdivはfor eachループから生成されるため、 read more..
リンクを区別して、読み取りのいずれかを判別する必要があります。 moreをクリックして、適切な $str
を2番目のdivに渡します。 jqueryを介して値を渡すことは可能ですか?またはそれを行うためのより良い方法はありますか?
私は自分の言葉が苦手なので混乱しているように聞こえるかもしれませんが、誰かが私の言っていることを理解しているなら、助けてください。
答え :
解決策:
代わりにjQueryを介してDOM操作を行うことは可能ですか?その場合は、次のコードに変更することをお勧めします:
$(document).ready(function () {
$.each($value, function(index) {
var desc = $value[index]["description"];
var container = $('<div></div>').append('<h1>Description</h1>');
if (desc.length < 50) {
container.append(desc);
} else {
var anchor = $('<a href="#" class="button" onclick="return false;"></a>')
//cache the rest of the description using .data()
.data('desc', desc.substr(50, desc.length))
.click(function () {
//using jQuery UI for a dialog
$('#genericDialog').html($(this).data('desc')).dialog();
})
.text("Read more...").wrap('<p></p>');
container.append(desc.substr(0, 50));
container.append(anchor);
}
//append the container to the dom
container.appendTo("#contactHolders");
});
});
これが私が使用したhtmlです:
<div id="contactHolders">
</div>
<div id="genericDialog"></div>
このコードが機能することを確認したい場合は、ここでフィドルを作成しました。 jQueryUIダイアログのドキュメントはこちらです。
答え :
解決策:
最も簡単な方法の1つは、長いテキストを非表示のブロックにキーイングpすることです。あなたはforeachにいます:
$str=$data['description'];
$substr=substr($str,0,50);
echo $substr.'<p class=button><a href=#>read more...<a/><p/>';
echo '<p class="full-desc">'.$str.'</p>';
次に、 p
をcssで非表示にします:
.full-desc {dis p lay:none; }
そしてjavascriptの値を取得します:
$( "。button")。click(function(){
$( "。contactArea")。html($(this).next()。text());
//cssを中心に
centerPo p u p();
// p o pupをロードします
loadPo p u p();
});
答え :
解決策:
あなたの質問を正しく理解していれば、もちろんこれを達成する方法はたくさんあります。
1つの解決策は、html 5データ属性を使用して、押されたリンクを識別する値を渡すことです。
続きを読むリンクに追加できます:
<p class=button data-descriptionId = [PHP code to write the Id]><a href=#>read more...<a/><p/>
次に、jQueryで、リンクのクリックされたイベントをフックし、値を取得して、ajax呼び出しを介してコンテンツをロードできます。データをどのようにロードするのかわかりませんが、次のようになります。
$(document).ready(function(){
$(".button").click(function(){
event.preventDefault();
var id = $(this).attr("data-decriptionId");
//Make ajax call to the database see jQuery documentation for $.ajax passing inn the idvariable
});
//set this as callback function to put the result in the second div
function SetData(data){
$(".contactArea").text(data);
}
});
ajaxを実行するには、ここ
のドキュメントを呼び出します。それでうまくいくはずです。
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。