我想将div的可见性更改为可见,默认情况下它是隐藏的

时间:2011-11-17 13:02:01

标签: javascript javascript-events

这是我的代码,我将div visibilty设置为默认隐藏

<div id="addComments" align="center" style="width:300px; height:300px; visibility:hidden">
    <button>Submit</button>
    <button>Cancel</button>
</div>

但是当我点击此链接时,我想将div的可见性更改为可见。

<a href="javascript:addMethod()">+ Create a new comment</a>

此链接背后的方法如下:

function addMethod() {
    try {

        var obj = document.getElementById('addComments');
        obj.visibility = "visible";
        var mashupId = _settings.ratedObjectId;
        var tenantId = _settings.tenantId;
        var comments = null;
        var user = { "Id": 2007, "FirstName": "Ufone", "LastName": "", "EmailAddress": "ahmed.ali@vidizmo.com509" };
        var ratingInfo = {
            "Id": 0,
            "TenantId": tenantId,
            "UserId": user.Id,
            "FirstName": user.FirstName,
            "LastName": user.LastName,
            "Rating": 0,
            "EmailAddress": user.EmailAddress,
            "Comments": comments,
            "RatedObjectTypeInfo": { "Id": 2 },
            "RatingTypeInfo": { "Id": 1 },
            "MashupRatingInfo": { "MashupMetaInfoId": mashupId },
            "RatedObjectId": mashupId,
            "TotalCount": 0
        };

        var url = "http://services.farooq.tv/RatingInfoManagementService/RatingInfoManagementService.svc/ajax/AddRating";

        $.ajax({
            type: "POST",
            url: url,
            data: JSON.stringify(ratingInfo),
            contentType: "application/json",
            timeout: 5000,
            success: function (d) {
                console.log(d);
                if (d == true) {

                    alert("Thank you");
                }
                else {

                    alert("Rewrite");
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr);
            }



        });

    }
    catch (err) {

        alert(err);}

}

请帮助我开始编程。

3 个答案:

答案 0 :(得分:3)

处理css时,请使用“style.visibility”(即obj.style.visibility =“visible”)

答案 1 :(得分:1)

更改

var obj = document.getElementById('addComments');
obj.visibility = "visible";

$('#addComments').show()

答案 2 :(得分:1)

我建议您使用其中一个可用的Javascript框架...例如,jQuery使生活更轻松..您可以使用那里提供的selectos来简单地选择您想要的任何元素..之后您可以轻松地处理CSS更改< / p>

例如:

$(selector).css("attribue","value");
or $(selector).show(); //or hide()